Parcel budget: Threshold file
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. - Count the numbers in
bestValuesthat are at leastk(greater than or equal tok). Store the count inatLeastK. - Complete
parcelBudgetThresholdFileso it returnsatLeastK. Return it, do not print it.
Example
If drawers is [[3, 10], [2, 2], [3, 4], [3, 5], [5, 8]] and k is 1, then bestValues is [0, 0] and parcelBudgetThresholdFile returns 0.
Rules
k(a whole number from 1 to 8) is the largest bag capacity to check in task 1, and also the threshold in task 2.
Test data
TEST DATA 1: drawers is [[4, 11], [5, 13], [1, 2], [5, 15], [6, 17]] and k is 1.
TEST DATA 2: drawers is [[1, 8], [1, 9], [1, 10], [2, 12]] and k is 7.
Examples
Input: drawers = [[4,11],[5,13],[1,2],[5,15],[6,17]] k = 1 Output: 1
bestValues is [0, 2]. Here k is 1. The numbers at least 1 are [2]. There are 1, so return 1.
Input: drawers = [[1,8],[1,9],[1,10],[2,12]] k = 7 Output: 7
bestValues is [0, 10, 19, 27, 31, 39, 39, 39]. Here k is 7. The numbers at least 7 are [10, 19, 27, 31, 39, 39, 39]. There are 7, so return 7.
Input: drawers = [] k = 6 Output: 0
bestValues is [0, 0, 0, 0, 0, 0, 0]. Here k is 6. The numbers at least 6 are []. There are 0, so 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.