Elevator Requests II - 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 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 <= 1091 <= requests.length <= 15000 <= start, requests[i] <= n - 1- All values in
requestsare distinct.
Approach Overview
Problem Overview: You are given a list of elevator requests—each specifying a target floor and direction (up/down). The elevator starts at floor 0 and must serve all requests in any order while minimizing the total distance traveled.
Approach 1: Brute Force - Permutations (O(n! · n))
Generate every possible order of serving the requests using recursion or itertools.permutations. For each permutation, simulate the elevator movement from floor to floor and compute the total distance. Track the minimum over all permutations. This approach is only feasible for very small n (<10). It demonstrates correctness but is impractical for real inputs.
Approach 2: Greedy - Nearest Floor First (O(n²))
At each step, scan all unserved requests and pick the one with the smallest absolute difference from the current floor. Move the elevator there and mark it served. This is intuitive and easy to implement but can be suboptimal when requests are clustered in opposite directions—it may cause unnecessary back-and-forth movement.
Approach 3: Optimal - Direction-Aware Priority Queue (O(n log n))
Maintain two heaps—one for upward requests and one for downward requests—keyed by floor number. While moving in a direction, serve all requests in that direction in order using the heap; when no more requests exist in the current direction, switch directions and pop from the other heap. This mimics real elevator logic and guarantees minimal travel distance because you never skip a request that lies ahead in your current path. The key insight is that serving all requests in the same direction before turning minimizes backtracking.
Recommended for interviews: Interviewers expect you to recognize that this is a scheduling problem and propose a greedy solution with heaps. Brute force shows you understand the problem but fails on complexity; nearest-floor shows awareness of greedy thinking but misses the directional optimization. The heap-based approach demonstrates that you can model real-world constraints efficiently and is the standard solution.
For more practice on similar patterns, explore greedy algorithms and heap/priority queue topics.
Solutions for this problem are being prepared.
Try solving it yourselfDetailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force | O(n! · n) | O(n) | Only for n ≤ 10 or verifying correctness |
| Greedy Nearest Floor | O(n²) | O(n) | When requests are sparse and directions don't conflict |
| Direction-Aware Priority Queue | O(n log n) | O(n) | General case – optimal and expected in interviews |
Frequently Asked Questions
Is Elevator Requests II easy or hard?
Elevator Requests II Python/Java solution
How to solve Elevator Requests II in O(n log n)?
What is the best approach for Elevator Requests II?
Is Elevator Requests II asked at Google/Amazon/Meta?
What data structure is used in Elevator Requests II?
What is the time complexity of Elevator Requests II?
Ready to solve this problem?
Practice Elevator Requests II with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor