1554. XOR Query Desk
A help desk hands out numbered visitor badges, and the same badge number may be present several times. Implement a class XorDesk that starts empty and supports three operations, where every number is an integer with 0 <= x < 2^30.
add(x): one more copy of badgexjoins the desk. Returns nothing.remove(x): if at least one copy ofxis present, remove exactly one copy and returntrue; otherwise change nothing and returnfalse.bestXor(x): return the largest value ofx ^ yover all badgesycurrently on the desk (copies count once, it is only a maximum), or-1if the desk is empty.
Each operation must run in O(30) time, and the structure uses O(30) memory per stored badge. At most 2000 calls are made in total.
Example 1
- Input:
- operations = ["XorDesk","add","add","bestXor","remove","bestXor"]arguments = [[],[5],[9],[3],[9],[3]]
- Output:
- [null,null,null,10,true,6]
- Explanation:
With badges 5 and 9, 3 ^ 5 = 6 and 3 ^ 9 = 10, so the best is 10; after removing 9 only 5 remains and the best is 6.
Example 2
- Input:
- operations = ["XorDesk","bestXor","add","remove","remove","bestXor"]arguments = [[],[4],[4],[4],[4],[1]]
- Output:
- [null,-1,null,true,false,-1]
- Explanation:
The empty desk gives -1; badge 4 is added and removed once, the second remove finds nothing and returns false, and the empty desk gives -1 again.
Constraints
0 ≤ x < 230
At most 2000 calls in total
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Input
- Each case is an operation log.
operationsnames the class first and then each method call;argumentsholds the arguments for each, in the same order. Your answer is one list with a result per operation -nullfor the constructor and for methods that return nothing. - Time per case
- Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms
Expected complexity
- Time
- O(30) per operation
- Space
- O(30 * number of stored badges)