Elevator Requests III Problem
Description
LeetCode Problem 4027.
You are given an integer n denoting the number of floors in a building, where the floors are numbered from 0 to n - 1.
You are also given an integer start and a 2D integer array requests, where requests[i] = [arrival_i, floor_i] indicates that a request for floor_i is made at time arrival_i.
At time 0, the elevator is at floor start.
At each second, the elevator may move up by 1 floor, move down by 1 floor, or remain on its current floor.
A request can be fulfilled only at or after its arrival time; it is fulfilled instantly when the elevator is on its requested floor at any time from its arrival time onward.
Return the minimum time needed to fulfill all requests.
Example 1:
Input: n = 9, start = 0, requests = [[0,8],[6,5]]
Output: 9
Explanation:
- Move from floor 0 (start) to floor 5 (requests[1][1]) in 5 seconds, reaching at time 5. Since requests[1][0] = 6, wait until time 6 to fulfill it.
- Move from floor 5 to floor 8 (requests[0][1]) in 3 seconds, fulfilling it at time 9.
Thus, all requests are fulfilled by time 9.
Example 2:
Input: n = 8, start = 5, requests = [[1,7],[7,3]]
Output: 7
Explanation:
- Move from floor 5 (start) to floor 7 (requests[0][1]) in 2 seconds, reaching at time 2. Since requests[0][0] = 1 has already passed, floor 7 is fulfilled at time 2.
- Move from floor 7 to floor 3 (requests[1][1]) in 4 seconds, reaching at time 6. Since requests[1][0] = 7, wait until time 7.
Thus, all requests are fulfilled by time 7.
Example 3:
Input: n = 7, start = 3, requests = [[0,5],[0,1],[6,3]]
Output: 8
Explanation:
- Move from floor 3 (start) to floor 5 (requests[0][1]) in 2 seconds, fulfilling it at time 2.
- Move from floor 5 to floor 1 (requests[1][1]) in 4 seconds, fulfilling it at time 6.
- Move from floor 1 to floor 3 (requests[2][1]) in 2 seconds, reaching at time 8. Its request arrived at requests[2][0] = 6, so floor 3 is fulfilled at time 8.
Thus, all requests are fulfilled by time 8.
Constraints:
1 <= n <= 10^91 <= requests.length <= 16requests[i] == [arrival_i, floor_i]0 <= arrival_i <= 10^90 <= start, floor_i <= n - 1
Solution
Approach: Bitmask DP
The building can be huge, but there are at most 16 requests, so only the order in which requests are served matters. This is a travelling-salesman style problem with arrival times.
Let f[mask][j] be the earliest time at which all requests in mask are fulfilled, with request j the last one. For mask containing j, let prev = mask without j:
- If
previs empty, the elevator comes fromstart:max(|start - floor_j|, arrival_j). - Otherwise try every
kinprevas the previous request:max(f[prev][k] + |floor_k - floor_j|, arrival_j), and keep the minimum.
The answer is the minimum of f[full][j] over all j.
Complexity: time O(m^2 * 2^m), space O(m * 2^m), where m = requests.length.
class Solution:
def elevatorRequests(self, n: int, start: int, requests: List[List[int]]) -> int:
m = len(requests)
f = [[0] * m for _ in range(1 << m)]
for i in range(1 << m):
for j in range(m):
if i >> j & 1:
f[i][j] = float('inf')
i0 = i ^ (1 << j)
if i0 == 0:
d = abs(start - requests[j][1])
f[i][j] = min(f[i][j], max(d, requests[j][0]))
else:
for j0 in range(m):
if j0 != j and (i >> j0 & 1):
d = abs(requests[j0][1] - requests[j][1])
f[i][j] = min(
f[i][j], max(f[i0][j0] + d, requests[j][0])
)
return min(f[(1 << m) - 1][j] for j in range(m))