A shipping fleet's dispatch office keeps a ledger mapping numeric dock identifiers to the identifier of whichever vessel currently occupies that dock. Over the course of a shift, three kinds of instructions arrive: SET dock vessel records that vessel now occupies dock (overwriting whatever vessel, if any, was previously recorded there); GET dock asks which vessel currently occupies dock, reported as -1 if the dock is currently unoccupied; and REMOVE dock frees dock, so it becomes unoccupied (doing nothing if it was already unoccupied). Process every instruction in order and, for each GET, report its answer.
q, the number of instructions.q lines is one instruction: SET dock vessel, GET dock, or REMOVE dock.For every GET instruction, print its answer on its own line, in the order the GET instructions appear. Print nothing for SET or REMOVE.
Example 1
Input
6 SET 1 10 SET 2 20 GET 1 REMOVE 1 GET 1 GET 2
Expected
10 -1 20
Explanation
After SET 1 10 and SET 2 20, GET 1 reports 10. REMOVE 1 frees dock 1, so the next GET 1 reports -1. GET 2 is unaffected and still reports 20.
Example 2
Input
5 GET 5 SET 5 100 SET 5 200 GET 5 REMOVE 10
Expected
-1 200
Explanation
GET 5 before anything is set reports -1. SET 5 100 then SET 5 200 overwrites dock 5 twice, so the next GET 5 reports the latest value, 200. REMOVE 10 targets a dock that was never set, so it is a harmless no-op and produces no output.
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 →