BSRC-P3

Parcel van capacity

Practice 3 of 5Binary search8 tests

The problem

A parcel office must send a row of parcels, with weights parcels, in the order they were booked. One van leaves each day. It carries the next few parcels in the row, as long as their total weight is not more than its capacity. Parcels cannot be split, and their order cannot change.

Return the smallest whole-number capacity that sends every parcel within days days.

min_capacity([3, 2, 2, 4, 1, 4], 3)   # 6: [3, 2], [2, 4], [1, 4]
min_capacity([5, 1, 4], 1)            # 10
min_capacity([1, 9, 1, 9, 1], 2)      # 11: [1, 9, 1], [9, 1]

Limits

  • 1 to 50,000 parcels, each weighing from 1 to 500,000.
  • days is at least 1. The large tests must finish within a few seconds.

Builds on BSRC-4 Searching on the answer.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

8 tests, not run yet
Test results
#TestResultReads · time
1The exampleNot run
2One dayNot run
3A day for every parcelNot run
4More days than parcelsNot run
5Ten parcels, five daysNot run
6Order mattersNot run
750,000 parcels, 100 daysNot run
8One very heavy parcelNot run