BackmediumArraysuncategorizedmedium

Optimal Resource Allocation Solution

Problem Statement

Given an array of storage units where each unit is represented as an array of three integers [oxygen, water, food], and a target array required = [req_oxygen, req_water, req_food], find the minimum number of storage units needed such that the total sum of each resource from selected units is at least the required amount. Return -1 if it is impossible.

Example 1
Input
units = [[10, 5, 3], [5, 10, 5]], required = [15, 15, 8]
Output
[[10, 5, 3], [5, 10, 5]]

Explanation: Selecting both storage units provides total oxygen: 10+5=15, water: 5+10=15, food: 3+5=8, meeting all required resource amounts.

Example 2
Input
units = [[5, 5, 5], [5, 5, 5]], required = [5, 5, 5]
Output
[[5, 5, 5]]

Explanation: Selecting a single storage unit [5, 5, 5] fulfills the required amounts of oxygen (5), water (5), and food (5) using the minimum number of units.

Constraints

  • 1 <= storageUnitCount <= 100
  • 1 <= resourceCount <= 3
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 Allocation — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(n * reqO * reqW * reqF)
|
SpaceO(reqO * reqW * reqF)

Problem Description

Given an array of storage units where each unit is represented as an array of three integers [oxygen, water, food], and a target array required = [req_oxygen, req_water, req_food], find the minimum number of storage units needed such that the total sum of each resource from selected units is at least the required amount. Return -1 if it is impossible.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Optimal Resource Allocation"

medium

WHY DOES IT MATTER?

Multi‑dimensional covering problems appear whenever a system must satisfy several constraints simultaneously (e.g., capacity planning, budget allocation, or service‑level agreements). Mastering this pattern teaches you to convert combinatorial explosion into tractable state‑space DP.

OPTIMIZATION CHALLENGE

The key insight is to bound the DP dimensions by the required amounts and to store only the minimum count per state. By capping each dimension at its target (using max(0, target‑sum)), we avoid exponential blow‑up and achieve pseudo‑polynomial time.

REAL-WORLD CONNECTION

Think of a disaster‑relief logistics hub: each truck carries a certain amount of oxygen, water, and food. The hub must dispatch the fewest trucks to meet the needs of a shelter. The DP models the hub’s decision process, ensuring minimal resource waste while guaranteeing safety.

When coding, always clamp the DP indices to the target values; this prevents array‑out‑of‑bounds and dramatically reduces memory. Also, initialize the DP with INF and set dp[0][0][0]=0, then iterate units in reverse to enforce 0‑1 usage.

COMPLEXITY AT A GLANCE

⏱ Time:O(n * reqO * reqW * reqF)
💾 Space:O(reqO * reqW * reqF)

Core Theory — Why This Approach?

The problem is a three‑dimensional variant of the classic "minimum subset sum" or "covering knapsack" problem. Each storage unit contributes a vector (oxygen, water, food) and we must select the smallest number of vectors whose component‑wise sum dominates a target vector. A naive exhaustive search enumerates all 2^n subsets, which explodes even for moderate n. The optimal paradigm treats the requirement vector as a bounded state space and applies dynamic programming (DP) or BFS over that space: dp[o][w][f] stores the minimum units needed to achieve at least o oxygen, w water, and f food. By iterating over each unit and updating the DP in reverse order, we guarantee each unit is used at most once, achieving a pseudo‑polynomial solution that scales with the product of the three required amounts rather than with 2^n. This DP is analogous to multi‑dimensional shortest‑path where each edge (unit) adds a fixed weight in three dimensions, and we seek the shortest path to any state that meets or exceeds the target.

Interview Questions on This Problem

Q1How would you modify the DP solution if each storage unit could be used unlimited times (unbounded supply)?

Switch to an unbounded knapsack DP by iterating the resource dimensions in forward order for each unit, allowing the same unit to contribute multiple times; this reduces to a classic unbounded multi‑dimensional DP where dp[o][w][f] = min(dp[o][w][f], 1 + dp[max(0, o‑ox)][max(0, w‑wat)][max(0, f‑food)]) for each unit.

Q2Explain how you could apply a BFS/shortest‑path approach instead of DP, and what the trade‑offs are.

Treat each state (o,w,f) as a node and each unit as an edge that moves to a new state with increased resources. BFS from (0,0,0) guarantees the first time we reach a state meeting the target we used the minimum number of units because each edge has equal weight (1). BFS can be more memory‑efficient if we prune states exceeding the target, but it may explore many states before reaching the goal, whereas DP fills the entire table deterministically.

Q3In a real‑time system where resources arrive continuously, how would you adapt the algorithm to maintain the current optimal count without recomputing from scratch?

Maintain the DP table incrementally: when a new unit arrives, update dp in reverse order using the unit’s vector, which only improves entries that can benefit from the new unit. This incremental update runs in O(reqO*reqW*reqF) per arrival and preserves the optimal count at any moment.

Examples

Example 1

Input

units = [[10, 5, 3], [5, 10, 5]], required = [15, 15, 8]

Output

[[10, 5, 3], [5, 10, 5]]

Explanation: Selecting both storage units provides total oxygen: 10+5=15, water: 5+10=15, food: 3+5=8, meeting all required resource amounts.

Example 2

Input

units = [[5, 5, 5], [5, 5, 5]], required = [5, 5, 5]

Output

[[5, 5, 5]]

Explanation: Selecting a single storage unit [5, 5, 5] fulfills the required amounts of oxygen (5), water (5), and food (5) using the minimum number of units.

Constraints

  • 1 <= storageUnitCount <= 100
  • 1 <= resourceCount <= 3

Optimal Approach & Strategy

Use a 3‑dimensional DP where each cell stores the minimum number of units needed to achieve that resource combination, updating it for each unit in reverse order to enforce 0‑1 selection.

Brute Force Approach

Enumerate every subset of storage units, sum their resources, and keep the smallest subset that meets the target. This is O(2^n) and quickly becomes infeasible.

Verified Code Solutions

JavaScript Solution
Time: O(n * reqO * reqW * reqF)
// Requires complete re-specification before implementation

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.