Smallest Conveyor Load Limit for Sequential Parcels
Heaps, Sorting & SearchAuto-graded · 19 test casesLast updated
Find the minimum per-day load limit that moves an ordered list of parcels in at most a given number of days, using binary search on the answer with a greedy feasibility check.
A warehouse has a queue of parcels that must be sent out by truck, one trip per day. Each parcel has a weight, and the parcels have to be sent in the order they appear in the queue.
You are given the parcel weights in queue order and the number of days available. Return the smallest daily load limit that allows every parcel to be sent within that many days.
Rules
- On each day the truck takes a contiguous block of parcels, beginning with the first parcel that has not been sent yet. Parcels cannot be split, skipped, or rearranged.
- The combined weight of the parcels taken on a single day must not exceed the load limit.
- Every parcel must be sent within
daysdays. Using fewer days is allowed. - The load limit is a positive integer. Return the smallest limit for which this is possible.
Function
Implement smallest_load_limit(weights, days):
weights: list of integers, whereweights[i]is the weight of thei-th parcel in queue order.days: integer, the maximum number of days allowed.- Returns: an integer, the minimum feasible daily load limit.
Constraints
1 <= len(weights) <= 10^51 <= weights[i] <= 10^41 <= days <= len(weights)- The total weight is at most
10^9, so the answer fits in a signed 32-bit integer.
Examples
Example 1
Input: smallest_load_limit([4, 8, 2, 5, 7, 3, 6], 3)
Output: 14
Example 2
Input: smallest_load_limit([9, 1, 1, 1], 2)
Output: 9
Example 3
Input: smallest_load_limit([5, 2, 6], 1)
Output: 13
Hints
Hint 1
Flip the question: instead of finding the limit directly, ask how many days a given limit would need.
Hint 2
For a fixed limit, loading parcels greedily (start a new day only when the next parcel would overflow) gives the fewest days. Larger limits never need more days, so the feasible limits form a suffix of the possible values.
Hint 3
Binary search between max(weights) (the heaviest parcel must fit) and sum(weights) (everything in one trip), using the first-true pattern: hi = mid on success, lo = mid + 1 on failure.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Rather than searching for the limit directly, answer the reverse question: given a limit, how many days are needed? This is easy to compute greedily, and the answer never increases as the limit grows. That makes binary search on the limit possible.
Feasibility check. Walk through the parcels, adding each one to the current day's load. If adding the next parcel would exceed the limit, start a new day with that parcel. Count the days used.
Search range.
- Lower bound is
max(weights): a parcel cannot be split, so the heaviest one must fit on some day. This also guarantees the greedy check never places an over-limit parcel on a day. - Upper bound is
sum(weights): everything fits in one trip, which is always allowed becausedays >= 1.
Binary search. While lo < hi, test mid. If it needs at most days days, it works, so set hi = mid. Otherwise set lo = mid + 1. When the loop ends, lo is the smallest feasible limit.
Why it is correct
- Monotonicity: if a limit works, any larger limit works too, because the same daily blocks still fit. So feasible limits form a suffix of the range, and the first feasible one is found by binary search.
- Greedy is optimal: after any number of days, the greedy schedule has sent at least as many parcels as any other valid schedule. By induction, if greedy is at position
g >= s(another schedule's position), the other schedule's next block starting atsfits within the limit, so its part fromgonward also fits. Greedy takes the longest block that fits, so it stays ahead. Hence greedy uses the fewest days. - The upper bound is always feasible, so the range never becomes empty and the invariant "the answer is in
[lo, hi]" holds.
Complexity
- Time:
O(n log(S - M)), whereSis the total weight andMthe maximum weight. WithS <= 10^9this is about 30 passes over the list. - Space:
O(1)extra.
Reference solution
from typing import List
def smallest_load_limit(weights: List[int], days: int) -> int:
def days_needed(limit: int) -> int:
d = 1
load = 0
for w in weights:
if load + w > limit:
d += 1
load = w
else:
load += w
return d
lo, hi = max(weights), sum(weights)
while lo < hi:
mid = (lo + hi) // 2
if days_needed(mid) <= days:
hi = mid
else:
lo = mid + 1
return lo