KatInterview

Minimize Maximum Delivery Time Across Warehouses

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

Last 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) <= 2000
  • 1 <= capacities[i] <= 10^6
  • 1 <= 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.