[LeetCode] Elevator Requests I

4020. Elevator Requests I

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int elevatorRequests(int n, vector<int>& requests) {
int res = 0, at = 0;
for(int x : requests) {
res += abs(at - x);
at = x;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/03/PS/LeetCode/elevator-requests-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.