Minimum Containers for Target Volume
Dynamic ProgrammingAuto-graded · 23 test casesLast updated
Find the minimum combination of reusable containers to reach an exact target total volume, or return None if impossible.
You are tasked with filling a storage tank to an exact volume. You have access to an unlimited supply of containers, where each unique size is given in an array canisters.
Write a function that returns a list containing the minimum total number of containers needed to reach the exact volume specified by target.
- If there are multiple valid choices that yield the minimum count, you may return any valid combination.
- If it is impossible to sum up to the exact
targetvolume using the given container sizes, returnNone. - If
targetis0, return an empty list[].
Constraints
1 <= len(canisters) <= 501 <= canisters[i] <= 10^4- All integers in
canistersare distinct. 0 <= target <= 10^4
Examples
Example 1
Input: min_canisters_to_target([1, 5, 10, 25], 30)
Output: [5, 25]
Example 2
Input: min_canisters_to_target([2], 3)
Output: None
Example 3
Input: min_canisters_to_target([4, 6, 9], 15)
Output: [6, 9]
Hints
Hint 1
A greedy strategy of choosing the largest container first will fail for cases like target = 12 with sizes [9, 6, 1]. What algorithm considers all optimal subproblem choices?
Hint 2
Think of this as the classic Coin Change problem, but instead of returning just the count, you need to reconstruct the actual items used.
Hint 3
Maintain an array `dp` where `dp[v]` is the minimum containers to form volume `v`, and a tracking array `prev` to store which container size achieved that minimum for volume `v`.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem can be framed as the standard Coin Change (Unbounded Knapsack) problem, with an added requirement to reconstruct the chosen set of items.
A standard greedy choice (taking the largest item that fits) fails in arbitrary systems. For instance, with canisters = [9, 6, 1] and target = 12, a greedy pick yields 9 + 1 + 1 + 1 (4 containers), whereas the optimal choice is 6 + 6 (2 containers).
To solve this correctly:
- Define
dp[v]as the minimum number of containers required to reach exactly volumev. - Initialize
dp[0] = 0and all other entries to infinity. - Use a tracking array
previous[v]to record the container size used to reach volumevoptimally. - For each volume
amountfrom1totarget, iterate through all container sizes: $$\text{dp}[\text{amount}] = \min_{size \le \text{amount}} (\text{dp}[\text{amount} - size] + 1)$$ - If
dp[target]remains infinite, the target is unreachable, so returnNone. - Otherwise, backtrack from
targetdown to0using thepreviousarray to build the list of containers used.
Complexity Analysis
- Time Complexity: $\mathcal{O}(N \cdot T)$, where $N$ is the number of container sizes (
len(canisters)) and $T$ is thetargetvolume. In the worst case ($50 \times 10^4 = 5 \times 10^5$), this takes a fraction of a second. - Space Complexity: $\mathcal{O}(T)$ to store the DP dynamic state array and the reconstruction array.
Python Reference Solution
from typing import List, Optional
def min_canisters_to_target(canisters: List[int], target: int) -> Optional[List[int]]:
if target == 0:
return []
inf = target + 1
best = [inf] * (target + 1)
previous = [None] * (target + 1)
best[0] = 0
for amount in range(1, target + 1):
for size in canisters:
if size <= amount and best[amount - size] + 1 < best[amount]:
best[amount] = best[amount - size] + 1
previous[amount] = size
if best[target] == inf:
return None
result = []
amount = target
while amount:
size = previous[amount]
result.append(size)
amount -= size
return result