Parcel-delivery app~20 min2s · 256 MBtarget O(n log T) time, O(1) spacebinary-search-answer
You work on a delivery network's warehouse system. A hub has several dock bays, and each bay moves one pallet at a time. Bay i needs a warm-up before its first pallet, and then spends the same number of minutes on every pallet.
The shift lead asks: "If all bays start together, how soon can the whole backlog be moved?" The hub has up to 100,000 bays and a backlog of up to a billion pallets.
Write fewest_minutes(minutes_per_pallet, warmup, pallets).
minutes_per_pallet[i] is how long bay i needs for one pallet.warmup[i] is the minutes bay i spends getting ready. It moves nothing during that time.pallets is the number of pallets in the backlog.k-th pallet at minute warmup[i] + k × minutes_per_pallet[i].pallets pallets.Input: minutes_per_pallet = [3, 5, 4], warmup = [4, 0, 2], pallets = 9
Output: 15Try a number of minutes and count what the bays can move. More minutes never move fewer pallets, so we look for the smallest number of minutes that reaches 9.
| Minutes | Pallets moved by each bay | Total | At least 9? |
|---|---|---|---|
| 13 | [3, 2, 2] | 7 | no |
| 14 | [3, 2, 3] | 8 | no |
| 15 | [3, 3, 3] | 9 | yes |
14 minutes move too few pallets, and 15 minutes are enough, so the answer is 15.
Input: minutes_per_pallet = [10], warmup = [0], pallets = 1
Output: 10One bay with no warm-up needs 10 minutes for one pallet.
Input: minutes_per_pallet = [2, 2], warmup = [100, 0], pallets = 5
Output: 10The second bay moves all five pallets alone in 10 minutes. The first bay is still warming up for 100 minutes.
1 ≤ len(minutes_per_pallet) ≤ 100,000, and warmup has the same length.1 ≤ minutes_per_pallet[i] ≤ 1,000,0000 ≤ warmup[i] ≤ 1,000,0001 ≤ pallets ≤ 1,000,000,000Moving the pallets one at a time takes up to a billion steps, and each step picks a bay. That is too slow for a 2-second limit. The answer itself can reach 10^15 minutes, so you cannot try every minute either. But the count of pallets moved only grows with the time, so you can search the time with about 50 checks.