KatInterview

Minimize the Maximum Delivery Time Across Routes

Heaps, Sorting & SearchAuto-graded · 17 test cases
MediumAuto-gradedHeaps, Sorting & Searchbinary searchgreedysorting

Last 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 <= 1000
  • 1 <= n <= 2000 where n = len(travel_times)
  • 1 <= travel_times[i] <= 10^6
  • travel_times may 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.