Loading...
Loading...
Keep a list of distinct numbers. add x appends x if it is not present and prints 1, otherwise prints 0. remove x prints 0 if x is absent; otherwise it overwrites x's place with the last number of the list, removes the last place and prints 1. at i prints the number at position i of the list (positions start at 0). All operations must be fast.
add x, remove x or at i.at i has a valid position iExample 1
Input
7 add 10 add 20 add 30 add 40 remove 10 at 0 at 2
Output
1 1 1 1 1 40 30
After removing 10, the last number 40 moves to position 0.
Example 2
Input
3 add 5 add 5 at 0
Output
1 0 5
Try the problem first. Open one hint at a time.
Use a list for positions and a dictionary from number to position.
On remove, move the last number into the freed position and fix its dictionary entry.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor