Parcel budget: Positive proof strip
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. - Create a list
positiveValuesthat keeps only the numbers inbestValuesthat are greater than 0, in the same order. - Complete
parcelBudgetPositiveProofStripso it returnspositiveValues. Return it, do not print it.
Example
If drawers is [[4, 3], [7, 7], [4, 5], [8, 10], [4, 7]] and k is 4, then bestValues is [0, 0, 0, 0, 7] and parcelBudgetPositiveProofStrip returns [7].
Rules
- Keep repeated numbers.
- If no number is greater than 0, return [].
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 [[1, 0], [3, 3], [1, 10], [3, 5], [3, 6]] and k is 7.
TEST DATA 2: drawers is [[1, 8], [7, 7], [7, 8], [7, 9]] and k is 1.
Examples
Input: drawers = [[1,0],[3,3],[1,10],[3,5],[3,6]] k = 7 Output: [10,10,10,16,16,16,21]
bestValues is [0, 10, 10, 10, 16, 16, 16, 21]. Keep only numbers greater than 0, without changing their order. Return [10, 10, 10, 16, 16, 16, 21].
Input: drawers = [[1,8],[7,7],[7,8],[7,9]] k = 1 Output: [8]
bestValues is [0, 8]. Keep only numbers greater than 0, without changing their order. Return [8].
Input: drawers = [] k = 1 Output: []
bestValues is [0, 0]. Keep only numbers greater than 0, without changing their order. Return [].
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.