Minimize Maximum Delivery Time Across Warehouses
Heaps, Sorting & SearchAuto-graded · 17 test casesLast updated
Tests binary search on the answer to find the minimum possible maximum delivery time when assigning shipments to warehouses with limited daily capacity.
Minimize Maximum Delivery Time Across Warehouses
You are coordinating shipments across a set of warehouses. Each warehouse has a processing speed: warehouse i can fulfill at most capacities[i] shipments per day.
You have n shipments to assign (one shipment per unit). Each shipment must be assigned to exactly one warehouse. A warehouse that is assigned x shipments takes ceil(x / capacities[i]) days to fulfill all of them.
Your goal is to distribute all n shipments across the warehouses such that the maximum fulfillment time across all warehouses is minimized. Return that minimum possible maximum fulfillment time.
Notes:
- Every shipment must be assigned (total assignments must equal
n). - A warehouse may be assigned zero shipments (it contributes 0 days to the maximum).
- You must use all warehouses (but assigning zero is allowed).
- Each warehouse must be assigned a non-negative integer number of shipments.
Constraints
1 <= len(capacities) <= 20001 <= capacities[i] <= 10^61 <= n <= 10^9
Examples
Example 1
Input: min_max_delivery_days([10, 5, 2], 20)
Output: 2
Example 2
Input: min_max_delivery_days([3], 9)
Output: 3
Example 3
Input: min_max_delivery_days([1, 1, 1, 1], 5)
Output: 2
Hints
Hint 1
If you fix a target number of days D, can you greedily check whether it's possible to assign all n shipments within D days?
Hint 2
For a fixed D days, warehouse i can handle at most capacities[i] * D shipments — sum these up and compare to n.
Hint 3
Binary search over D: the answer is at least 1 and at most ceil(n / max(capacities)), so search in that range.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach: Binary Search on the Answer
Key Insight
Rather than searching over all possible assignments (which would be combinatorially explosive), we binary search over the answer — the number of days D.
Feasibility check: For a fixed D, warehouse i can handle at most capacities[i] * D shipments within D days (assign as many as it can hold, taking exactly ceil(x / capacities[i]) <= D days). So the distribution is feasible if and only if:
sum(capacities[i] * D for all i) >= n
This check is O(W) where W = len(capacities).
Search Bounds
- Lower bound:
lo = 1(optimistic — maybe one day is enough). - Upper bound:
hi = ceil(n / max(capacities))— assign allnshipments to the fastest warehouse alone; this is always feasible.
The true answer lies in [lo, hi]. Binary search finds it in O(log(hi)) iterations.
Correctness
- If
Ddays is feasible, anyD' > Dis also feasible (monotone property), so binary search applies. - The feasibility sum is exact: we're checking whether the total capacity across all warehouses for
Ddays meets or exceedsn. The greedy assignment (fill each warehouse to itsD-day limit) achieves the sum exactly, so feasibility ↔ sum ≥ n.
Complexity
- Time:
O(W * log(ceil(n / max_cap))). WithW ≤ 2000andn ≤ 10^9,log(n) ≈ 30, so roughly 60,000 operations — very fast. - Space:
O(1)extra.
Reference Solution
from typing import List
import math
def min_max_delivery_days(capacities: List[int], n: int) -> int:
def feasible(days: int) -> bool:
total = 0
for c in capacities:
total += c * days
if total >= n:
return True
return False
lo = 1
hi = math.ceil(n / max(capacities))
while lo < hi:
mid = (lo + hi) // 2
if feasible(mid):
hi = mid
else:
lo = mid + 1
return lo