A service lift in a building can stop at floors 0 through maxFloor. It begins at a given starting floor and receives a sequence of signed move commands. Each command is an integer delta: a positive value goes up, a negative value goes down. The lift may not pass the ends of its shaft, so after each command its floor is limited to the range [0, maxFloor] - if a command would take it below 0 it stops at floor 0, and if it would take it above maxFloor it stops at maxFloor.
Line 1: two integers maxFloor and start (with 0 <= start <= maxFloor).
Line 2: an integer n, the number of commands.
Line 3: n space-separated integers, the deltas in order.
A single integer: the lift's final floor.
Example 1
Input
5 2 4 3 -10 2 1
Expected
3
Explanation
From 2: +3 -> 5 (top), -10 clamps to 0, +2 -> 2, +1 -> 3. Final floor 3.
Example 2
Input
10 0 3 5 5 5
Expected
10
Explanation
From 0: +5 -> 5, +5 -> 10 (top), +5 clamps to 10. Final floor 10.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →