Parcel budget: Total receipt
Stuck or curious how this works? Visualize 0/1 Knapsack ↗
Inputs
drawers is a list of parcels. Each row is [weight, value].
Your tasks
- Create a list
bestValues. Each row ofdrawersis a parcel: [weight, value]. The weight is positive, and the value is 0 or more. A bag can hold a total weight up to its capacity. For every capacity from 0 throughk, find the largest total value you can pack. You may use each parcel at most once. You may leave parcels out. Put one answer per capacity inbestValues, including capacity 0. - Add up every number in
bestValues. Store the sum intotal. - Complete
parcelBudgetTotalReceiptso it returnstotal. Return it, do not print it.
Example
If drawers is [[3, 2], [2, 10], [3, 4], [5, 7], [4, 7]] and k is 1, then bestValues is [0, 0] and parcelBudgetTotalReceipt returns 0.
Rules
k(a whole number from 1 to 8) is the largest bag capacity to check in task 1.
Test data
TEST DATA 1: drawers is [[7, 6], [1, 17], [1, 2], [1, 19], [7, 10]] and k is 3.
TEST DATA 2: drawers is [[1, 8], [7, 7], [1, 10], [1, 11]] and k is 8.
Examples
Input: drawers = [[7,6],[1,17],[1,2],[1,19],[7,10]] k = 3 Output: 93
bestValues is [0, 19, 36, 38]. Add its numbers to get 93. Return 93.
Input: drawers = [[1,8],[7,7],[1,10],[1,11]] k = 8 Output: 206
bestValues is [0, 11, 21, 29, 29, 29, 29, 29, 29]. Add its numbers to get 206. Return 206.
Input: drawers = [] k = 2 Output: 0
bestValues is [0, 0, 0]. Add its numbers to get 0. Return 0.
Input limits
- 0 <= input size <= 64.
- k is a whole number from 1 to 8. Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- Input numbers have no decimal part. They range from -1,000,000 to 1,000,000. Follow any extra input rules in task 1.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(n*k); extra memory, including the helper list, O(k). n is the input length. These labels describe how work and memory grow with the input.
Practice focus
Build bestValues first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.