1126. Sweeping Elevator
An elevator parked at floor current has a list of floors where passengers are waiting, given as requests. It first travels upward, stopping at every requested floor that is at or above its position in ascending order. After the last such floor it reverses once and travels downward, stopping at the remaining requested floors in descending order.
Return the floors in the exact order the elevator serves them. All requested floors are distinct, so a floor never appears twice. The floor the elevator is on counts as part of the upward sweep.
Constraints: 1 <= n <= 1000, 0 <= requests[i], current <= 10^4 and requests are distinct. Sorting once and splitting at current gives O(n log n) time and O(n) space.
Example 1
- Input:
- requests = [5,1,9,3,7], current = 4
- Output:
- [5,7,9,3,1]
- Explanation:
Going up from floor 4 serves 5, 7, 9; going back down serves 3 and 1.
Example 2
- Input:
- requests = [2,8,6], current = 10
- Output:
- [8,6,2]
- Explanation:
No request is at or above floor 10, so the elevator goes straight down: 8, 6, 2.
Constraints
1 ≤ n ≤ 1000
0 ≤ requests[i], current ≤ 104
requests are distinct
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n log n)
- Space
- O(n)