Skip to content
BytePatterns

Minimum Daily Capacity

MediumSearching#binary-search-on-answer#greedy-check~30m

Problem

A conveyor carries packages in the order they are listed, and every package must ship within a given number of days. A day's load is a run of packages taken from the front, and no day may exceed the belt's capacity. Find the smallest capacity that still finishes on time.

Examples

Input:  weights = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], days = 5
Output: 15
Why:    1-5, 6-7, 8, 9, 10 fills exactly five days
Input:  weights = [3, 2, 2, 4, 1, 4], days = 3
Output: 6
Input:  weights = [5], days = 1
Output: 5
Why:    edge case, the capacity can never be below the heaviest package

Hints

0 / 3

Stuck on the idea rather than the code? Binary Search on Answer covers it.