Minimum Possible Maximum Waiting Time Problem
Description
LeetCode Problem 4009.
You are given an integer array demand, where demand[i] is the amount of fuel required by the i^th car.
You are also given an integer array fuel of length 2. There are exactly two fuel dispensers, numbered 0 and 1, where fuel[j] is the initial amount of fuel available in dispenser j.
Cars are allowed to start refueling in increasing index order. Car 0 becomes allowed at time 0, and for each i > 0, car i becomes allowed exactly when car i - 1 starts refueling.
The refueling process follows these rules:
- Each dispenser can serve at most one car at a time.
- When a car becomes allowed, you must choose a dispenser with at least
demand[i]fuel remaining. If both dispensers have enough fuel remaining, you may choose either of them, regardless of when they become free. - The car waits until the chosen dispenser becomes free and starts refueling immediately. It cannot switch dispensers or intentionally wait after the chosen dispenser becomes free.
- When a car starts refueling, the remaining fuel in the chosen dispenser decreases by
demand[i], and the dispenser remains occupied fordemand[i]seconds. - Once started, refueling cannot be interrupted.
- If neither dispenser has at least
demand[i]fuel remaining when caribecomes allowed, the process terminates and no further cars can be served.
The waiting time of a car is the time between when it becomes allowed to start refueling and when it actually starts.
Return the minimum possible value of the maximum waiting time among all served cars over all assignments that maximize the number of served cars. If no car can be served, return -1.
Example 1:
Input: demand = [6,8,4,6,5], fuel = [16,13]
Output: 6
Explanation:
The following assignment serves all five cars:
Car Becomes allowed at Starts refueling at Dispenser used Remaining fuel before start (dispenser 0, dispenser 1) Waiting time
0 0 0 0 (16, 13) 0
1 0 0 1 (10, 13) 0
2 0 6 0 (10, 5) 6
3 6 10 0 (6, 5) 4
4 10 10 1 (0, 5) 0
Thus, all five cars are served, and the maximum waiting time is 6.
To serve all five cars, dispenser 0 must serve the cars with demands 6, 4, and 6, while dispenser 1 must serve the cars with demands 8 and 5. Therefore, car 2 must wait until time 6 for dispenser 0 to become free, so no assignment serving all five cars can have a maximum waiting time less than 6.
Example 2:
Input: demand = [10,15], fuel = [12,17]
Output: 0
Explanation:
- At time 0, Car 0 becomes allowed and starts refuelling using dispenser 0.
- Car 1 becomes allowed at time 0 (when Car 0 starts) and immediately starts refuelling using dispenser 1.
- Both cars start without waiting, so the maximum waiting time is 0.
Example 3:
Input: demand = [10,5], fuel = [8,8]
Output: -1
Explanation:
- At time 0, Car 0 becomes allowed. However, neither dispenser has enough fuel to serve it, so the process terminates immediately.
- No car is served, so the answer is -1.
Constraints:
1 <= demand.length <= 501 <= demand[i] <= 20fuel.length == 21 <= fuel[i] <= 50
Solution
Approach: Memoized Search
Cars are handled in index order, and car i becomes allowed exactly when car i - 1 starts. So the only choice is which dispenser each car uses, and we want the largest number of served cars first and the smallest maximum waiting time second.
Describe the situation when car i becomes allowed by the fuel left in each dispenser and by how many seconds from now each dispenser becomes free (w0 and w1, 0 if it is free already). If car i uses dispenser j:
- its waiting time is
w_j; - the next car becomes allowed
w_jseconds later, and dispenserjis now busy fordemand[i]more seconds; - the other dispenser's remaining busy time shrinks by
w_j(but not below0).
Every busy time is at most 20, because it is either demand of the last car on that dispenser or something smaller. The remaining fuel of dispenser 1 follows from dispenser 0 and the cars already served, so the state has about n * 51 * 21 * 21 values.
For each state we keep the pair (cars served, max waiting time) and prefer more cars, then a smaller wait. Choosing dispenser j gives (1 + served, max(w_j, wait)) from the following state. If neither dispenser has enough fuel the search stops with (0, 0). If no car is served at all, return -1.
Complexity: time and space O(n * F * W^2), where F <= 50 is the fuel and W <= 20 is the largest demand.
from functools import lru_cache
class Solution:
def minMaxWaitingTime(self, demand: List[int], fuel: List[int]) -> int:
n = len(demand)
# State: car i is allowed now, the dispensers hold r0 and r1 fuel and become free
# w0 and w1 seconds from now (0 if already free). Returns (served cars, max waiting time).
@lru_cache(None)
def dfs(i: int, r0: int, r1: int, w0: int, w1: int) -> Tuple[int, int]:
if i == n:
return (0, 0)
d = demand[i]
best = (0, 0)
if r0 >= d:
c, m = dfs(i + 1, r0 - d, r1, d, max(0, w1 - w0))
cand = (c + 1, max(w0, m))
if cand[0] > best[0] or (cand[0] == best[0] and cand[1] < best[1]):
best = cand
if r1 >= d:
c, m = dfs(i + 1, r0, r1 - d, max(0, w0 - w1), d)
cand = (c + 1, max(w1, m))
if cand[0] > best[0] or (cand[0] == best[0] and cand[1] < best[1]):
best = cand
return best
served, wait = dfs(0, fuel[0], fuel[1], 0, 0)
return wait if served > 0 else -1