Elevator Requests II Problem
Description
LeetCode Problem 4023.
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, representing the floor where the elevator begins, and an integer array requests, where requests[i] is a floor that the elevator is requested to reach. All floors in requests are distinct.
At time 0, the elevator is on floor start, and all requests are made simultaneously.
During each second before all requests are fulfilled, the elevator moves exactly one floor, either up or down. A request is fulfilled instantly when the elevator reaches its requested floor. If start appears in requests, that request is fulfilled at time 0.
For each second that a request remains unfulfilled, you receive 1 penalty. Equivalently, a request fulfilled at time t contributes t to the total penalty.
Return the minimum total penalty required to fulfill all requests.
Example 1:
Input: n = 6, start = 4, requests = [1,5]
Output: 6
Explanation:
- Move from floor 4 (start) to floor 5 in 1 second. Penalty for floor 5 is 1.
- Move from floor 5 to floor 1 in 4 seconds. Penalty for floor 1 is 5.
Thus, the total penalty is 1 + 5 = 6.
Example 2:
Input: n = 8, start = 3, requests = [3,7,1]
Output: 10
Explanation:
- Floor 3 (start) is fulfilled instantly. Penalty for floor 3 is 0.
- Move from floor 3 to floor 1 in 2 seconds. Penalty for floor 1 is 2.
- Move from floor 1 to floor 7 in 6 seconds. Penalty for floor 7 is 8.
Thus, the total penalty is 0 + 2 + 8 = 10.
Example 3:
Input: n = 10, start = 5, requests = [0,2,9]
Output: 22
Explanation:
- Move from floor 5 (start) to floor 2 in 3 seconds. Penalty for floor 2 is 3.
- Move from floor 2 to floor 0 in 2 seconds. Penalty for floor 0 is 5.
- Move from floor 0 to floor 9 in 9 seconds. Penalty for floor 9 is 14.
Thus, the total penalty is 3 + 5 + 14 = 22.
Constraints:
1 <= n <= 10^91 <= requests.length <= 15000 <= start, requests[i] <= n - 1- All values in
requestsare distinct.
Solution
Approach: Interval Dynamic Programming
Only the order of the requested floors matters, and the elevator passes through every floor between two stops. So the floors served at any moment always form a contiguous interval of the sorted requested floors, and it contains start.
Sort the distinct floors of requests together with start. Let f[l][r][side] be the least penalty so far when the floors l..r are served and the elevator stands at the left (side = 0) or right (side = 1) end. Rather than recording each fulfilment time, charge every move: a move of distance D adds D for each request still waiting, which is D * (total - served).
From [l, r] extend to l - 1 or r + 1, coming from either end, and add that charge. Start from f[st][st] = 0 and grow the interval until it covers every floor; the answer is the better of the two ends. Rolling the table over the interval length keeps the memory at O(m).
Complexity: time O(m^2), space O(m), where m <= 1501 is the number of distinct floors.
class Solution:
def elevatorRequests(self, n: int, start: int, requests: List[int]) -> int:
pts = sorted(set(requests) | {start})
m = len(pts)
is_req = set(requests)
pc = [0] * (m + 1) # pc[i]: number of requested floors among pts[:i]
for i in range(m):
pc[i + 1] = pc[i] + (1 if pts[i] in is_req else 0)
total = len(requests)
st = pts.index(start)
inf = float('inf')
# The floors served so far always form an interval [l, r] of pts that contains start.
# f0[l] / f1[l]: least penalty with the elevator at the left / right end of [l, l + length - 1].
# Every move of distance D adds D for each request still waiting.
f0 = [inf] * m
f1 = [inf] * m
f0[st] = f1[st] = 0
for length in range(1, m):
g0 = [inf] * m
g1 = [inf] * m
for l in range(max(0, st - length + 1), min(st, m - length) + 1):
r = l + length - 1
a, b = f0[l], f1[l]
rem = total - (pc[r + 1] - pc[l])
if l > 0:
g0[l - 1] = min(a + (pts[l] - pts[l - 1]) * rem, b + (pts[r] - pts[l - 1]) * rem)
if r + 1 < m:
g1[l] = min(a + (pts[r + 1] - pts[l]) * rem, b + (pts[r + 1] - pts[r]) * rem)
f0, f1 = g0, g1
return min(f0[0], f1[0])