BackmediumDynamic Programminguncategorizedmedium

Optimal Resource Distribution Solution

Problem Statement

You are given three types of resources: oxygen, food, and water, with 27, 18, and 32 units available respectively. There are 7 sectors, each with a specific requirement of resources. The goal is to maximize the total resource utilization by allocating the resources to the sectors.

Example 1
Input
sectors = [[1, 1, 1], [2, 2, 2], [4, 3, 5], [1, 1, 1], [2, 2, 2], [1, 1, 1], [4, 3, 5]]
Output
[1, 1, 1, 2, 2, 1, 4, 3, 5]

Explanation: Step-by-step: We first sort the sectors array based on the total resource requirements in descending order. Then, we iterate over the sorted sectors array and allocate the resources to each sector. We keep track of the remaining resources and allocate the maximum possible resources to each sector. Finally, we return the array of allocated resources.

Example 2
Input
sectors = [[1, 1, 1], [2, 2, 2], [4, 3, 5], [1, 1, 1], [2, 2, 2], [1, 1, 1], [4, 3, 5]]
Output
[4, 3, 5, 2, 2, 1, 1]

Explanation: Step-by-step: We first sort the sectors array based on the total resource requirements in descending order. Then, we iterate over the sorted sectors array and allocate the resources to each sector. We keep track of the remaining resources and allocate the maximum possible resources to each sector. Finally, we return the array of allocated resources.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Optimal Resource Distribution — Problem Statement & Solution Guide

Dynamic ProgrammingMediumMixed
TimeO(n·O·F·W)
|
SpaceO(O·F·W)

Problem Description

You are given three types of resources: oxygen, food, and water, with 27, 18, and 32 units available respectively. There are 7 sectors, each with a specific requirement of resources. The goal is to maximize the total resource utilization by allocating the resources to the sectors.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Optimal Resource Distribution"

medium

WHY DOES IT MATTER?

Multi‑dimensional knapsack captures real‑world allocation problems where multiple scarce resources must be balanced simultaneously; mastering this pattern equips engineers to design optimal schedulers, budgeters, and load balancers.

OPTIMIZATION CHALLENGE

The key insight is to treat the three resource capacities as dimensions of a DP state and to prune states that are strictly dominated, turning an exponential search into a pseudo‑polynomial algorithm.

REAL-WORLD CONNECTION

Think of a distributed cloud system allocating CPU, memory, and network bandwidth to micro‑services; each service (sector) needs a specific mix, and the orchestrator must maximize overall utilization without exceeding any resource pool.

When coding, always iterate resource dimensions in reverse order during DP updates to prevent using the same sector multiple times, and use a 1‑D flattened array or hashmap to keep memory usage low.

COMPLEXITY AT A GLANCE

⏱ Time:O(n·O·F·W)
💾 Space:O(O·F·W)

Core Theory — Why This Approach?

The problem is a classic instance of the multi‑dimensional (or multi‑constraint) knapsack where each sector is an item that consumes a vector of three resources (oxygen, food, water) and yields a profit equal to the total units allocated to it. A naive greedy allocation—e.g., filling sectors in order of smallest total requirement—fails because the resource constraints are inter‑dependent; satisfying one sector may exhaust a critical resource needed by a later, higher‑value sector, leading to sub‑optimal total utilization. The optimal paradigm is dynamic programming that enumerates feasible resource states across sectors, storing the maximum utilized resources for each (oxygen, food, water) tuple, which guarantees the global optimum by considering all combinations while avoiding exponential recomputation.

For n sectors and capacities O, F, W, a three‑dimensional DP table dp[i][o][f][w] (or a compressed version) records the best utilization after processing the first i sectors with o oxygen, f food, and w water remaining. Transition: either skip the sector or allocate it if its requirement fits, updating the state accordingly. This DP runs in O(n·O·F·W) time and O(O·F·W) space, which is tractable for the modest capacities given (27, 18, 32) but would be infeasible for large limits without further optimizations such as state compression, pruning dominated states, or meet‑in‑the‑middle techniques.

Interview Questions on This Problem

Q1How would you model the Optimal Resource Distribution problem as a multi‑dimensional knapsack and what DP state representation would you use?

Model each sector as an item with a 3‑dimensional weight vector (oxygen, food, water) and profit equal to the sum of its allocated resources. Use a DP state dp[o][f][w] = maximum total utilized resources achievable with o oxygen, f food, and w water remaining, iterating over sectors and updating states in reverse to avoid reuse.

Q2In a fintech platform, you need to allocate limited capital across multiple loan portfolios with three risk constraints. Which algorithmic pattern from this problem applies and why?

The same multi‑constraint knapsack pattern applies because each loan portfolio consumes a vector of risk exposures (e.g., credit, market, operational) and contributes profit. DP over the three risk dimensions ensures the allocation respects all constraints while maximizing return, mirroring the resource distribution solution.

Q3A high‑growth startup wants to scale the solution to thousands of sectors and larger resource caps. What optimization would you suggest beyond the basic DP?

Suggest state compression using a hash map to store only reachable states, pruning dominated states (where one state uses more resources but yields less profit), or applying a meet‑in‑the‑middle approach that splits sectors into two halves and combines partial solutions, reducing time from O(n·O·F·W) to roughly O(2^{n/2}) for very large n.

Examples

Example 1

Input

sectors = [[1, 1, 1], [2, 2, 2], [4, 3, 5], [1, 1, 1], [2, 2, 2], [1, 1, 1], [4, 3, 5]]

Output

[1, 1, 1, 2, 2, 1, 4, 3, 5]

Explanation: Step-by-step: We first sort the sectors array based on the total resource requirements in descending order. Then, we iterate over the sorted sectors array and allocate the resources to each sector. We keep track of the remaining resources and allocate the maximum possible resources to each sector. Finally, we return the array of allocated resources.

Example 2

Input

sectors = [[1, 1, 1], [2, 2, 2], [4, 3, 5], [1, 1, 1], [2, 2, 2], [1, 1, 1], [4, 3, 5]]

Output

[4, 3, 5, 2, 2, 1, 1]

Explanation: Step-by-step: We first sort the sectors array based on the total resource requirements in descending order. Then, we iterate over the sorted sectors array and allocate the resources to each sector. We keep track of the remaining resources and allocate the maximum possible resources to each sector. Finally, we return the array of allocated resources.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Optimal Approach & Strategy

Use a three‑dimensional DP that records the best utilization for each (oxygen, food, water) state while iterating through sectors, achieving O(n·O·F·W) time. Compress the DP to a hashmap or 1‑D array to keep space manageable.

Brute Force Approach

Enumerate every subset of sectors and for each check if the total resource consumption fits within the three caps, tracking the best utilization. This requires O(2^n) time, which is infeasible for larger n.

Verified Code Solutions

JavaScript Solution
Time: O(n·O·F·W)
function solution(sectors, oxygen, food, water) {
   // Sort the sectors array based on the total resource requirements in descending order
   sectors.sort((a, b) => (b[0] + b[1] + b[2]) - (a[0] + a[1] + a[2]));

   let allocatedResources = [];
   let remainingOxygen = oxygen;
   let remainingFood = food;
   let remainingWater = water;

   for (let sector of sectors) {
      let oxygenRequired = Math.min(sector[0], remainingOxygen);
      let foodRequired = Math.min(sector[1], remainingFood);
      let waterRequired = Math.min(sector[2], remainingWater);

      allocatedResources.push([oxygenRequired, foodRequired, waterRequired]);

      remainingOxygen -= oxygenRequired;
      remainingFood -= foodRequired;
      remainingWater -= waterRequired;
   }

   return allocatedResources;
}

Asked in Top Tech Interviews

uncategorizedmediumgeneric

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.