Snapshot Set
DatabricksPhone screenMedium
SnapshotSet is a set of integers that remembers the order in which values were added. Removing a value and adding it again counts as a new addition, so the value moves to the end of that order.
Part 1: Ordered set
Implement the four basic methods of SnapshotSet.
add(value)insertsvalueand returnstrue. Ifvalueis already in the set, it returnsfalseand changes nothing.remove(value)deletesvalueand returnstrue, or returnsfalseifvalueis not in the set.contains(value)returns whethervalueis in the set.values()returns a new list of the current values, oldest addition first.
- Example 1
- init
[]- operations
[["add",[3]],["add",[1]],["add",[3]],["add",[2]],["remove",[1]],["contains",[1]],["values",[]],["add",[1]],["values",[]]]- Output
[true,true,false,true,true,false,[3,2],true,[3,2,1]]
Why: Adding 3 twice inserts it once, so the second
addreturnsfalse. Removing 1 leaves[3, 2]; adding 1 again treats it as newly added, so it lands at the end.
Constraints
0 <= value <= 10^91 <= len(operations) <= 10^4