Minimize the Maximum Delivery Time Across Routes
Heaps, Sorting & SearchAuto-graded · 17 test casesLast updated
Tests binary search on the answer combined with greedy feasibility checking to minimize the maximum delivery time when assigning parcels to couriers.
Minimize the Maximum Delivery Time Across Routes
You manage a delivery network. There are k couriers available, and n delivery routes, each with a known travel time (in minutes). You must assign every route to exactly one courier. Each courier then drives all of their assigned routes sequentially, so their total workload is the sum of the travel times of routes assigned to them.
Your goal is to assign all routes to the k couriers so that the maximum workload among all couriers is minimized. Return that minimized maximum workload.
Assignment rules:
- Every route must be assigned to exactly one courier.
- A courier may receive zero or more routes.
- Routes assigned to a single courier do not need to be contiguous in the original list; you may freely choose which routes each courier takes.
- If
k >= n, each courier can take at most one route, so the answer is the maximum travel time among all routes.
Constraints
1 <= k <= 10001 <= n <= 2000wheren = len(travel_times)1 <= travel_times[i] <= 10^6travel_timesmay contain duplicates.- All values are integers.
Examples
Example 1
Input: minimize_max_delivery_time([3, 6, 2, 8, 4], 3)
Output: 9
Example 2
Input: minimize_max_delivery_time([10, 10, 10, 10], 2)
Output: 20
Example 3
Input: minimize_max_delivery_time([5], 5)
Output: 5
Hints
Hint 1
If you fix a candidate value M for the maximum workload, can you greedily check whether k couriers are enough to cover all routes without any courier exceeding M?
Hint 2
Sort the routes in descending order before the greedy check — greedily assign each route to a courier only if it fits within the remaining capacity, and use as few couriers as needed. What are the valid bounds for binary search?
Hint 3
The answer lies between max(travel_times) (lower bound: at least one route must be assigned somewhere) and sum(travel_times) (upper bound: one courier takes everything). Binary search within this range.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Binary Search on the Answer
Instead of directly searching for the optimal assignment, we flip the problem: binary search on the answer value M (the candidate maximum workload), and for each M ask the yes/no question: "Is it feasible to assign all routes to k couriers such that no courier's total exceeds M?"
This transforms a hard combinatorial optimization into a sequence of easy greedy checks.
Feasibility Check (Greedy)
Given a limit M:
- Sort routes in descending order (large routes first — this is a standard greedy trick that minimizes wasted capacity).
- Walk through routes, accumulating a running load for the current courier.
- Whenever adding the next route would exceed
M, open a new courier. - If we never need more than
kcouriers, returnTrue; otherwiseFalse.
Why is greedy correct here? Because we are asking "can we do it at all?" (not asking for the actual assignment), and the greedy first-fit-decreasing strategy is optimal for the feasibility question: sorting largest-first never makes feasibility worse, and packing greedily wastes no couriers unnecessarily.
Binary Search Bounds
- Lower bound
lo = max(travel_times): the answer can never be less than the single largest route, since some courier must take it. - Upper bound
hi = sum(travel_times): one courier could take all routes; this is always feasible.
We binary search for the smallest M in [lo, hi] that is feasible, which is a classic find leftmost true binary search.
Correctness
- The feasibility predicate is monotone: if
Mis feasible, so is anyM' > M. - Binary search therefore correctly locates the minimum feasible value.
Complexity
Let n = len(travel_times), S = sum(travel_times), X = max(travel_times).
- Binary search iterations: O(log(S − X)) ≤ O(log(n · 10^6)) ≈ O(log n + 20)
- Each feasibility check: O(n log n) (dominated by the sort; the sort can be hoisted outside the loop for O(n) per check, reducing overall to O(n log n + n log S))
- Overall time: O(n log n · log S) — well within limits for n ≤ 2,000.
- Space: O(n) for the sorted copy.
from typing import List
def minimize_max_delivery_time(travel_times: List[int], k: int) -> int:
def is_feasible(limit: int) -> bool:
couriers_needed = 1
current_load = 0
for t in sorted(travel_times, reverse=True):
if t > limit:
return False
if current_load + t > limit:
couriers_needed += 1
current_load = t
else:
current_load += t
return couriers_needed <= k
lo = max(travel_times)
hi = sum(travel_times)
while lo < hi:
mid = (lo + hi) // 2
if is_feasible(mid):
hi = mid
else:
lo = mid + 1
return lo