|
| 1 | +/** |
| 2 | + * 381. Insert Delete GetRandom O(1) - Duplicates allowed |
| 3 | + * https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * RandomizedCollection is a data structure that contains a collection of numbers, possibly |
| 7 | + * duplicates (i.e., a multiset). It should support inserting and removing specific elements |
| 8 | + * and also reporting a random element. |
| 9 | + * |
| 10 | + * Implement the RandomizedCollection class: |
| 11 | + * - RandomizedCollection() Initializes the empty RandomizedCollection object. |
| 12 | + * - bool insert(int val) Inserts an item val into the multiset, even if the item is already |
| 13 | + * present. Returns true if the item is not present, false otherwise. |
| 14 | + * - bool remove(int val) Removes an item val from the multiset if present. Returns true if |
| 15 | + * the item is present, false otherwise. Note that if val has multiple occurrences in the |
| 16 | + * multiset, we only remove one of them. |
| 17 | + * - int getRandom() Returns a random element from the current multiset of elements. The |
| 18 | + * probability of each element being returned is linearly related to the number of the same |
| 19 | + * values the multiset contains. |
| 20 | + * |
| 21 | + * You must implement the functions of the class such that each function works on average O(1) |
| 22 | + * time complexity. |
| 23 | + * |
| 24 | + * Note: The test cases are generated such that getRandom will only be called if there is at |
| 25 | + * least one item in the RandomizedCollection. |
| 26 | + */ |
| 27 | + |
| 28 | +var RandomizedCollection = function() { |
| 29 | + this.values = []; |
| 30 | + this.map = new Map(); |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * @param {number} val |
| 35 | + * @return {boolean} |
| 36 | + */ |
| 37 | +RandomizedCollection.prototype.insert = function(val) { |
| 38 | + this.values.push(val); |
| 39 | + const values = this.map.get(val) || new Set(); |
| 40 | + values.add(this.values.length - 1); |
| 41 | + this.map.set(val, values); |
| 42 | + return values.size === 1; |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * @param {number} val |
| 47 | + * @return {boolean} |
| 48 | + */ |
| 49 | +RandomizedCollection.prototype.remove = function(val) { |
| 50 | + if (!this.map.has(val)) return false; |
| 51 | + const values = this.map.get(val); |
| 52 | + const index = values.values().next().value; |
| 53 | + values.delete(index); |
| 54 | + if (index < this.values.length - 1) { |
| 55 | + const last = this.values.pop(); |
| 56 | + this.values[index] = last; |
| 57 | + const lastValues = this.map.get(last); |
| 58 | + lastValues.delete(this.values.length); |
| 59 | + lastValues.add(index); |
| 60 | + } else { |
| 61 | + this.values.pop(); |
| 62 | + } |
| 63 | + if (!values.size) { |
| 64 | + this.map.delete(val); |
| 65 | + } |
| 66 | + return true; |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * @return {number} |
| 71 | + */ |
| 72 | +RandomizedCollection.prototype.getRandom = function() { |
| 73 | + return this.values[Math.floor(Math.random() * this.values.length)]; |
| 74 | +}; |
0 commit comments