Pack Expedition Gear Within Weight and Volume Limits
Dynamic ProgrammingAuto-graded · 16 test casesLast updated
Tests the 2-constraint (weight + volume) knapsack variant using DP with memoization or tabulation to maximize the total utility of selected gear items.
Expedition Gear Packing
You are preparing for a wilderness expedition and must pack a selection of gear items into a single backpack. The backpack has two hard limits: a maximum weight capacity and a maximum volume capacity.
Each gear item has a weight (in grams), a volume (in cubic centimetres), and a utility score (a positive integer representing how useful it is on the trip). You may take at most one copy of each item (0/1 knapsack). Your goal is to choose a subset of items that fits within both limits while maximising the total utility score.
Task
Implement max_expedition_utility(weights, volumes, utilities, max_weight, max_volume).
weights[i],volumes[i], andutilities[i]describe the i-th item.- Return the maximum total utility achievable without exceeding either capacity.
Constraints
1 ≤ len(weights) == len(volumes) == len(utilities) ≤ 1001 ≤ weights[i] ≤ 2001 ≤ volumes[i] ≤ 2001 ≤ utilities[i] ≤ 10000 ≤ max_weight ≤ 20000 ≤ max_volume ≤ 2000- If
max_weight == 0ormax_volume == 0, no item can be packed; return0. - If no item fits within both limits, return
0.
Examples
Example 1
Input: max_expedition_utility([3, 5, 2], [4, 3, 2], [10, 7, 5], 6, 6)
Output: 15
Example 2
Input: max_expedition_utility([10], [10], [42], 5, 10)
Output: 0
Example 3
Input: max_expedition_utility([1, 2, 3, 4], [1, 2, 3, 4], [1, 4, 9, 16], 5, 5)
Output: 17
Hints
Hint 1
Extend the classic 1-D knapsack DP table to two dimensions: dp[w][v] = best utility using at most w grams and v cubic centimetres.
Hint 2
Iterate over items one at a time and update the DP table in reverse order for both capacity dimensions to avoid using an item more than once.
Hint 3
The final answer is dp[max_weight][max_volume] after processing all items.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a classic 2-constraint 0/1 knapsack problem. We extend the standard 1-D capacity DP to two dimensions.
DP Definition
Let dp[w][v] be the maximum utility achievable using items considered so far, with at most w grams of weight and v cubic centimetres of volume available.
Base case: dp[w][v] = 0 for all w, v (no items selected).
Transition: For each item i with weight wi, volume vi, utility ui:
dp[w][v] = max(dp[w][v], dp[w - wi][v - vi] + ui)
for w in [max_weight, wi] (reverse)
for v in [max_volume, vi] (reverse)
Iterating both dimensions in reverse ensures each item is counted at most once (the 0/1 property), exactly as in the standard 1-D knapsack.
Correctness
After processing all n items, dp[max_weight][max_volume] holds the optimal answer. Reverse iteration guarantees that when we update dp[w][v], the value dp[w - wi][v - vi] still reflects the state before considering item i, so we cannot "reuse" item i.
Edge cases handled automatically:
max_weight == 0ormax_volume == 0: the loops never execute an update, returning0.- Empty item list: the loop over items never runs, returning
0. - No item fits: none of the transitions fire for those items, so
dpstays0.
Complexity
| Value | |
|---|---|
| Time | O(n × max_weight × max_volume) |
| Space | O(max_weight × max_volume) |
With n ≤ 100, max_weight ≤ 2000, max_volume ≤ 2000, the worst case is 4 × 10⁸ operations — tight, but each inner step is a single comparison and potential integer assignment. In practice, constraints keep this manageable; for competitive settings one might add early-exit pruning.
Reference Solution
from typing import List
def max_expedition_utility(weights: List[int], volumes: List[int], utilities: List[int], max_weight: int, max_volume: int) -> int:
n = len(weights)
# dp[w][v] = max utility using capacity w grams and v cc
dp = [[0] * (max_volume + 1) for _ in range(max_weight + 1)]
for i in range(n):
wi, vi, ui = weights[i], volumes[i], utilities[i]
# Iterate in reverse to keep 0/1 property
for w in range(max_weight, wi - 1, -1):
for v in range(max_volume, vi - 1, -1):
dp[w][v] = max(dp[w][v], dp[w - wi][v - vi] + ui)
return dp[max_weight][max_volume]