Welcome to Subscribe On Youtube
4020. Elevator Requests I
Description
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 array requests, where requests represents the sequence of floor requests.
An elevator starts at floor 0 and follows these rules:
- The elevator moves one floor per second.
- The elevator serves requests in the given order.
- If the elevator is already on the requested floor, no movement is needed.
- After serving a request, the elevator immediately starts moving toward the next request.
Return the total time in seconds required to serve all requests.
Example 1:
Input: n = 5, requests = [2,1,4,3]
Output: 7
Explanation:
requests[0] = 2: Moving from floor 0 to floor 2 takes 2 seconds.requests[1] = 1: Moving from floor 2 to floor 1 takes 1 second.requests[2] = 4: Moving from floor 1 to floor 4 takes 3 seconds.requests[3] = 3: Moving from floor 4 to floor 3 takes 1 second.
The total time required is 2 + 1 + 3 + 1 = 7 seconds.
Example 2:
Input: n = 3, requests = [2,0,0]
Output: 4
Explanation:
requests[0] = 2: Moving from floor 0 to floor 2 takes 2 seconds.requests[1] = 0: Moving from floor 2 to floor 0 takes 2 seconds.requests[2] = 0: No movement is needed.
The total time required is 2 + 2 + 0 = 4 seconds.
Constraints:
1 <= n <= 1001 <= requests.length <= 1000 <= requests[i] <= n - 1
Solutions
Solution 1: Simulation
Thinking
The request order is fixed, so the elevator has no choice of permutation.
Travel time between consecutive requests is the absolute floor difference; the first leg from floor $0$ is exactly $\textit{requests}[0]$.
Summing those differences is the total time and needs only a linear scan.
The elevator starts at floor $0$ and serves requests in the given order. The travel time between two consecutive requests is the absolute difference of their floor numbers. The first request goes from floor $0$ to $\textit{requests}[0]$, which takes $\textit{requests}[0]$ seconds. Then we add the absolute differences of adjacent requests.
The time complexity is $O(m)$, and the space complexity is $O(1)$, where $m$ is the number of requests.
-
class Solution { public int elevatorRequests(int n, int[] requests) { int ans = requests[0]; for (int i = 1; i < requests.length; ++i) { ans += Math.abs(requests[i - 1] - requests[i]); } return ans; } } -
class Solution { public: int elevatorRequests(int n, vector<int>& requests) { int ans = requests[0]; for (int i = 1; i < requests.size(); ++i) { ans += abs(requests[i - 1] - requests[i]); } return ans; } }; -
class Solution: def elevatorRequests(self, n: int, requests: list[int]) -> int: return requests[0] + sum(abs(x - y) for x, y in pairwise(requests)) -
func elevatorRequests(n int, requests []int) int { ans := requests[0] for i, x := range requests[1:] { ans += abs(x - requests[i]) } return ans } func abs(x int) int { if x < 0 { return -x } return x } -
function elevatorRequests(n: number, requests: number[]): number { let ans: number = requests[0]; for (let i = 1; i < requests.length; ++i) { ans += Math.abs(requests[i] - requests[i - 1]); } return ans; }