← 返回 bloomberg 的题目列表Design a Lottery System with O(1) Delete
类型:online_judge
Problem
Design a lottery system maintaining a dynamic set S (elements can be integers or strings) supporting:
add(x): if x is not in the set, add it and return true; otherwise return false.
remove(x): if x is in the set, remove it and return true; otherwise return false.
draw(): return a uniformly random element from the current set.
Requirements:
The expected time complexity of all three operations must be O(1).
Provide the data structure design and key implementation details.
Example
Input
add 1
add 2
add 1
draw
remove 2
remove 3
draw
Output
true
true
false
(1 or 2)
true
false
1