Binary Search on the Answer: Batch Capacity

medium
Unit 13 · Heaps & Binary Search0/3 to milestone

You have jobs with sizes sizes that must be processed in order, packed into batches over days days (one batch per day, consecutive jobs per batch). What's the smallest daily capacity that finishes everything in time?

You can't easily compute it directly, but you can easily check a capacity: greedily pack jobs until the next one won't fit, and count the days. Feasibility is monotonic (bigger capacity → fewer days), so binary search over the capacity.

Implement min_capacity(sizes, days). The answer lies between max(sizes) and sum(sizes). Raise ValueError if days < 1. Your solution should be roughly O(nlog⁡(sum))O(n \log(\text{sum})).

Example: min_capacity([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5) → 15, with batches [1,2,3,4,5] [6,7] [8] [9] [10].

#binary-search#greedy2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Binary Search on the Answer: Batch Capacity · TechGradify