A ferry terminal installs a mechanical turnstile that displays a running headcount of passengers currently aboard. The turnstile starts at a factory-set calibration value. Dock staff issue a stream of commands to the turnstile: a passenger boards, a passenger disembarks, or the display is recalibrated back to its original starting value. After each command the display updates, and its new reading is logged.
Given the starting calibration value and the sequence of commands, report the turnstile's reading after every single command.
init, the turnstile's starting calibration value.q, the number of commands.q lines contains one command, exactly one of BOARD, DISEMBARK, or RESET.
BOARD increases the current reading by 1.DISEMBARK decreases the current reading by 1 (the reading may go negative; there is no lower bound).RESET sets the current reading back to init (the original starting value, not the value before the previous reset).Print a single line containing the q readings, in order, taken after each command is applied, separated by single spaces. If q is 0, print an empty line.
Example 1
Input
5 3 BOARD BOARD DISEMBARK
Expected
6 7 6
Explanation
The turnstile starts at 5. The first BOARD raises it to 6, the second BOARD raises it to 7, and DISEMBARK lowers it to 6, giving the reading sequence 6 7 6.
Example 2
Input
10 4 DISEMBARK DISEMBARK RESET BOARD
Expected
9 8 10 11
Explanation
Starting at 10, two DISEMBARK commands bring the reading to 9 then 8. RESET snaps it back to the original calibration value 10 (not to 7 or to any value derived from the last reading). The final BOARD then raises it to 11, giving 9 8 10 11.
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 →