Elevator Requests IV Problem
Description
LeetCode Problem 4029.
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 <= 500requests[i] == [arrival_i, floor_i]0 <= arrival_i <= 10^90 <= start, floor_i <= n - 1
Solution
Approach: Interval DP on the last visits
A request is fulfilled if the elevator is on its floor at some moment at or after its arrival, so what matters for each floor is its last visit. Merge requests on the same floor (keep the latest arrival) and sort the distinct floors.
Look at the order of the last visits. The floor visited last for the final time is where the elevator stops; before that, the elevator never leaves the range of floors it still has to serve. So floors are "finished" from the outside in: the next floor to finish is always the lowest or the highest unfinished one. The unfinished floors therefore always form a contiguous range [i, j] of the sorted floors.
Let left[i][j] be the earliest time at which all floors outside [i, j] are finished and the elevator stands on floor i - 1, and right[i][j] the same with the elevator on floor j + 1. From a state at time t and position pos, finishing the lowest floor takes max(t + |pos - floor_i|, arrival_i) (wait there if it arrived later), and likewise for the highest floor. The start state is the full range with the elevator on start at time 0, and the answer is the best time after the last floor is finished.
Complexity: time O(m^2), space O(m^2), where m is the number of requests.
class Solution:
def elevatorRequests(self, n: int, start: int, requests: List[List[int]]) -> int:
need = {}
for arrival, floor in requests:
need[floor] = max(need.get(floor, 0), arrival)
fl = sorted(need)
ar = [need[f] for f in fl]
k = len(fl)
INF = float('inf')
# left[i][j] / right[i][j]: earliest time with floors outside [i, j] served,
# the elevator standing on floor fl[i - 1] / fl[j + 1] (the one served last)
left = [[INF] * k for _ in range(k)]
right = [[INF] * k for _ in range(k)]
left[0][k - 1] = 0
ans = INF
for length in range(k, 0, -1):
for i in range(k - length + 1):
j = i + length - 1
for side in range(2):
t = (left if side == 0 else right)[i][j]
if t == INF:
continue
if length == k:
pos = start
else:
pos = fl[i - 1] if side == 0 else fl[j + 1]
a = max(t + abs(pos - fl[i]), ar[i]) # serve the lowest floor next
b = max(t + abs(pos - fl[j]), ar[j]) # serve the highest floor next
if length == 1:
ans = min(ans, a)
else:
left[i + 1][j] = min(left[i + 1][j], a)
right[i][j - 1] = min(right[i][j - 1], b)
return ans