
Leetcode Problem 380: Insert Delete GetRandom O(1)
Implement a RandomizedSet class that supports three operations, each in O(1) average time. insert(val) inserts the value into the set if not already present and returns true, or false if the value was already there. remove(val) removes the value from the set if present and returns true, or false if it was not found. getRandom() returns a random element from the current set with each element having an equal probability of being chosen. It is guaranteed that at least one element exists when getRandom is called.
Constraints:
insert, remove, and getRandom.getRandom is called.class RandomizedSet {
constructor(
private map: Map<number, number> = new Map(),
private list: number[] = []
) {}
insert(val: number): boolean {
if(this.map.has(val)) {
return false
}
const count = this.list.push(val)
this.map.set(val, count - 1)
return true
}
remove(val: number): boolean {
if(!this.map.has(val)) {
return false;
}
const index: number = this.map.get(val)!;
[this.list[index], this.list[this.list.length - 1]] = [this.list[this.list.length - 1], this.list[index]];
this.list.pop();
if (index < this.list.length) {
this.map.set(this.list[index], index);
}
this.map.delete(val);
return true;
}
getRandom(): number {
return this.list[Math.floor(Math.random() * this.list.length)]
}
}