|
| 1 | +// Source : https://leetcode.com/problems/insert-delete-getrandom-o1/ |
| 2 | +// Author : francisco |
| 3 | +// Date : 2024-01-16 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * Implement the RandomizedSet class: |
| 8 | + * |
| 9 | + * RandomizedSet() Initializes the RandomizedSet object. |
| 10 | + * bool insert(int val) Inserts an item val into the set if not present. Returns true if the |
| 11 | + * item was not present, false otherwise. |
| 12 | + * bool remove(int val) Removes an item val from the set if present. Returns true if the item |
| 13 | + * was present, false otherwise. |
| 14 | + * int getRandom() Returns a random element from the current set of elements (it's guaranteed |
| 15 | + * that at least one element exists when this method is called). Each element must have the same |
| 16 | + * probability of being returned. |
| 17 | + * |
| 18 | + * You must implement the functions of the class such that each function works in average O(1) time |
| 19 | + * complexity. |
| 20 | + * |
| 21 | + * Example 1: |
| 22 | + * |
| 23 | + * Input |
| 24 | + * ["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"] |
| 25 | + * [[], [1], [2], [2], [], [1], [2], []] |
| 26 | + * Output |
| 27 | + * [null, true, false, true, 2, true, false, 2] |
| 28 | + * |
| 29 | + * Explanation |
| 30 | + * RandomizedSet randomizedSet = new RandomizedSet(); |
| 31 | + * randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully. |
| 32 | + * randomizedSet.remove(2); // Returns false as 2 does not exist in the set. |
| 33 | + * randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2]. |
| 34 | + * randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly. |
| 35 | + * randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2]. |
| 36 | + * randomizedSet.insert(2); // 2 was already in the set, so return false. |
| 37 | + * randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return |
| 38 | + * 2. |
| 39 | + * |
| 40 | + * Constraints: |
| 41 | + * |
| 42 | + * -2^31 <= val <= 2^31 - 1 |
| 43 | + * At most 2 * 10^5 calls will be made to insert, remove, and getRandom. |
| 44 | + * There will be at least one element in the data structure when getRandom is called. |
| 45 | + ******************************************************************************************************/ |
| 46 | + |
| 47 | +export class RandomizedSet { |
| 48 | + list: number[]; |
| 49 | + map: Map<number, number>; |
| 50 | + |
| 51 | + constructor() { |
| 52 | + this.list = []; |
| 53 | + this.map = new Map(); |
| 54 | + } |
| 55 | + |
| 56 | + search(val: number): boolean { |
| 57 | + return this.map.has(val); |
| 58 | + } |
| 59 | + |
| 60 | + insert(val: number): boolean { |
| 61 | + if (this.search(val)) return false; |
| 62 | + |
| 63 | + this.list.push(val); |
| 64 | + this.map.set(val, this.list.length - 1); |
| 65 | + |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + remove(val: number): boolean { |
| 70 | + if (!this.search(val)) return false; |
| 71 | + |
| 72 | + const index: number = this.map.get(val) as number; |
| 73 | + |
| 74 | + this.list[index] = this.list[this.list.length - 1] as number; |
| 75 | + this.map.set(this.list[index] as number, index); |
| 76 | + this.list.pop(); |
| 77 | + this.map.delete(val); |
| 78 | + |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + getRandom(): number { |
| 83 | + const randomIndex: number = Math.floor(Math.random() * this.list.length); |
| 84 | + |
| 85 | + return this.list[randomIndex] as number; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Your RandomizedSet object will be instantiated and called as such: |
| 91 | + * var obj = new RandomizedSet() |
| 92 | + * var param_1 = obj.insert(val) |
| 93 | + * var param_2 = obj.remove(val) |
| 94 | + * var param_3 = obj.getRandom() |
| 95 | + */ |
0 commit comments