Skip to main content

Elevator Requests I - Solution & Explanation

EasyArraySimulation7 min read
Practice this problem

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 <= 100
  • 1 <= requests.length <= 100
  • 0 <= 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

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SimulationO(n^2)O(1)Small input size or quick prototype
Greedy with SortingO(n log n)O(1)When requests are unsorted and you need a balance
Single Pass SimulationO(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 is rated as an Easy problem with an 83.5% acceptance rate. It is a straightforward simulation task that requires understanding of basic iteration and conditional logic. The optimal solution is intuitive and can be implemented in a few lines of code, making it a good warm-up for more complex problems.
Elevator Requests I Python/Java solution
In Python, you can implement the single pass simulation with a simple for loop. In Java, use a for-each loop over the requests array. The logic is identical: track current floor and count, compute distances, and update accordingly. Both solutions run in O(n) time and O(1) space.
How to solve Elevator Requests I in O(n)?
To solve in O(n), maintain a variable for the current floor and a counter for served requests. For each request, calculate the absolute difference between the current floor and the request's start floor, then add the distance from start to end. If the total distance is within the allowed time, update the current floor to the end floor and increment the counter. This single pass avoids sorting and nested loops.
What is the best approach for Elevator Requests I?
The best approach is the single pass simulation, which runs in O(n) time and O(1) space. It iterates through each request once, updating the elevator's current floor and counting served requests based on whether the travel distance fits within the total time. This is optimal because it avoids unnecessary sorting or nested loops.
Is Elevator Requests I asked at Google/Amazon/Meta?
Elevator Requests I is a general simulation problem that tests basic algorithmic thinking. While not specifically tagged for these companies, similar simulation problems are common in technical interviews at top tech companies. The optimal O(n) approach demonstrates the kind of clean, efficient coding skills interviewers look for.
What data structure is used in Elevator Requests I?
The optimal solution uses no complex data structures—just basic variables to track the current floor and the count of served requests. This makes the solution extremely memory-efficient with O(1) space. Some alternative approaches might use sorting, which requires an array or list, but the single pass simulation avoids that.
What is the time complexity of Elevator Requests I?
The optimal solution has a time complexity of O(n), where n is the number of requests. This is achieved by a single pass simulation that processes each request in constant time. The space complexity is O(1) as no additional data structures are used.

Ready to solve this problem?

Practice Elevator Requests I with our built-in code editor and test cases.

Practice on FleetCode