Elevator Requests I - Solution & Explanation
Problem Statement
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
Approach Overview
Problem Overview: You are given a list of elevator requests, each with a start and end floor. The elevator starts at floor 0 and can move in either direction. Your task is to determine the maximum number of requests the elevator can serve without exceeding its capacity or violating any constraints. This is a classic simulation problem that tests your ability to model real-world scenarios with code.
Approach 1: Brute Force Simulation (O(n^2) time, O(1) space)
For each request, simulate the elevator's movement from its current floor to the request's start floor, then to the end floor. Track the total distance traveled and the number of requests served. This approach is straightforward but inefficient for large inputs because it recalculates the path for every request. Use it only when the input size is small or when you need a quick, simple solution. The time complexity is O(n^2) due to nested loops over requests, and space complexity is O(1) as no extra data structures are used.
Approach 2: Greedy with Sorting (O(n log n) time, O(1) space)
Sort the requests by their start floor, then process them in order. For each request, check if the elevator can reach the start floor from its current position within the given time. If yes, serve the request and update the elevator's position and the count. The key insight is that sorting by start floor minimizes the total travel distance, allowing you to serve more requests. This approach is optimal when the requests are not already sorted. Time complexity is O(n log n) due to sorting, space is O(1) if sorting in-place.
Approach 3: Single Pass Simulation (O(n) time, O(1) space) - Recommended
This is the optimal approach. Iterate through the requests once, maintaining the elevator's current floor and the count of served requests. For each request, compute the absolute difference between the current floor and the start floor, then add the distance from start to end. If the total distance does not exceed the elevator's total travel time, serve the request and update the current floor to the end floor. Otherwise, skip the request. The key insight is that you don't need to sort or backtrack; a single pass suffices because the elevator's movement is linear and you always serve the next request if possible. This approach is what interviewers expect—it shows you can optimize a simulation to O(n). It runs in O(n) time and O(1) space, making it scalable for large inputs.
Recommended for interviews: The single pass simulation is the go-to solution. It demonstrates your ability to reason about state and optimize away unnecessary work. While brute force shows you understand the problem, the O(n) solution proves you can write clean, efficient code under pressure. Interviewers at top companies like Google and Amazon value this kind of thinking. For more practice, check out our simulation and greedy topic pages.
Solution
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 requests[0], which takes 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.
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(n^2) | O(1) | Small input size or quick prototype |
| Greedy with Sorting | O(n log n) | O(1) | When requests are unsorted and you need a balance |
| Single Pass Simulation | O(n) | O(1) | General case, optimal for interviews |
Video Solution
Leetcode 4027 | Elevator Requests III | Leetcode weekly contest 515 | Dynamic Programming | Bitmasks • CodeWithMeGuys • 175 views views
Watch 1 more video solutions →Frequently Asked Questions
Is Elevator Requests I easy or hard?
Elevator Requests I Python/Java solution
How to solve Elevator Requests I in O(n)?
What is the best approach for Elevator Requests I?
Is Elevator Requests I asked at Google/Amazon/Meta?
What data structure is used in Elevator Requests I?
What is the time complexity of Elevator Requests I?
Ready to solve this problem?
Practice Elevator Requests I with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor