Parcel budget: Even-key census
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 even numbers in
bestValues. Store the count inevenCount. A number is even when it divides by 2 with no remainder. - Complete
parcelBudgetEvenKeyCensusso it returnsevenCount. Return it, do not print it.
Example
If drawers is [[1, 8], [3, 3], [1, 10], [7, 9], [1, 12]] and k is 8, then bestValues is [0, 12, 22, 30, 30, 30, 33, 33, 33] and parcelBudgetEvenKeyCensus returns 6.
Rules
- 0 counts as an even number.
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 [[6, 13], [3, 11], [6, 15], [1, 11], [1, 4]] and k is 6.
TEST DATA 2: drawers is [[1, 8], [1, 9], [7, 8], [1, 11]] and k is 6.
Examples
Input: drawers = [[6,13],[3,11],[6,15],[1,11],[1,4]] k = 6 Output: 4
bestValues is [0, 11, 15, 15, 22, 26, 26]. The even numbers are [0, 22, 26, 26]. There are 4, so return 4.
Input: drawers = [[1,8],[1,9],[7,8],[1,11]] k = 6 Output: 6
bestValues is [0, 11, 20, 28, 28, 28, 28]. The even numbers are [0, 20, 28, 28, 28, 28]. There are 6, so return 6.
Input: drawers = [] k = 1 Output: 2
bestValues is [0, 0]. The even numbers are [0, 0]. There are 2, so return 2.
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.