1140. Bus Stop Boarding
A shuttle bus starts empty with capacity seats and visits stops in order. At stop i, first off[i] riders get off. Then up to waiting[i] people try to board: min(waiting[i], free seats) of them board, and the rest are left behind for good, because they do not wait for another bus.
Both arrays have the same length, and the input guarantees that off[i] never exceeds the number of riders on board at that stop. Return the total number of people left behind over all stops. Keep a running count of riders on board and add up the leftovers in a single pass, which takes O(n) time and O(1) space.
Example 1
- Input:
- off = [0,2,1], waiting = [5,4,3], capacity = 6
- Output:
- 3
- Explanation:
Stop 0 boards 5, stop 1 frees 2 seats and boards 3 of 4 (1 left behind), stop 2 frees 1 seat and boards 1 of 3 (2 left behind), so 3 people are left behind.
Example 2
- Input:
- off = [0,0,0], waiting = [2,2,2], capacity = 4
- Output:
- 2
- Explanation:
The bus is full after stop 1, so the 2 people waiting at stop 2 are left behind and the answer is 2.
Constraints
1 ≤ n ≤ 1000, where n = off.length = waiting.length
0 ≤ waiting[i] ≤ 1000
0 ≤ off[i] ≤ riders on board
1 ≤ capacity ≤ 104
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)
- Space
- O(1)