|
| 1 | +# 381. Insert Delete GetRandom O(1) - Duplicates allowed |
| 2 | +`RandomizedCollection` is a data structure that contains a collection of numbers, possibly duplicates (i.e., a multiset). It should support inserting and removing specific elements and also reporting a random element. |
| 3 | + |
| 4 | +Implement the `RandomizedCollection` class: |
| 5 | +* `RandomizedCollection()` Initializes the empty `RandomizedCollection` object. |
| 6 | +* `bool insert(int val)` Inserts an item `val` into the multiset, even if the item is already present. Returns `true` if the item is not present, `false` otherwise. |
| 7 | +* `bool remove(int val)` Removes an item `val` from the multiset if present. Returns `true` if the item is present, `false` otherwise. Note that if `val` has multiple occurrences in the multiset, we only remove one of them. |
| 8 | +* `int getRandom()` Returns a random element from the current multiset of elements. The probability of each element being returned is **linearly related** to the number of the same values the multiset contains. |
| 9 | + |
| 10 | +You must implement the functions of the class such that each function works on **average** `O(1)` time complexity. |
| 11 | + |
| 12 | +**Note:** The test cases are generated such that `getRandom` will only be called if there is **at least one** item in the `RandomizedCollection`. |
| 13 | + |
| 14 | +#### Example 1: |
| 15 | +<pre> |
| 16 | +<strong>Input:</strong> |
| 17 | +["RandomizedCollection", "insert", "insert", "insert", "getRandom", "remove", "getRandom"] |
| 18 | +[[], [1], [1], [2], [], [1], []] |
| 19 | +<strong>Output:</strong> |
| 20 | +[null, true, false, true, 2, true, 1] |
| 21 | +<strong>Explanation:</strong> |
| 22 | +RandomizedCollection randomizedCollection = new RandomizedCollection(); |
| 23 | +randomizedCollection.insert(1); // return true since the collection does not contain 1. |
| 24 | + // Inserts 1 into the collection. |
| 25 | +randomizedCollection.insert(1); // return false since the collection contains 1. |
| 26 | + // Inserts another 1 into the collection. Collection now contains [1,1]. |
| 27 | +randomizedCollection.insert(2); // return true since the collection does not contain 2. |
| 28 | + // Inserts 2 into the collection. Collection now contains [1,1,2]. |
| 29 | +randomizedCollection.getRandom(); // getRandom should: |
| 30 | + // - return 1 with probability 2/3, or |
| 31 | + // - return 2 with probability 1/3. |
| 32 | +randomizedCollection.remove(1); // return true since the collection contains 1. |
| 33 | + // Removes 1 from the collection. Collection now contains [1,2]. |
| 34 | +randomizedCollection.getRandom(); // getRandom should return 1 or 2, both equally likely. |
| 35 | +</pre> |
| 36 | + |
| 37 | +#### Constraints: |
| 38 | +* <code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code> |
| 39 | +* At most <code>2 * 10<sup>5</sup></code> calls **in total** will be made to `insert`, `remove`, and `getRandom`. |
| 40 | +* There will be **at least one** element in the data structure when `getRandom` is called. |
| 41 | + |
| 42 | +## Solutions (Python) |
| 43 | + |
| 44 | +### 1. Solution |
| 45 | +```Python |
| 46 | +import random |
| 47 | + |
| 48 | + |
| 49 | +class RandomizedCollection: |
| 50 | + |
| 51 | + def __init__(self): |
| 52 | + self.numlist = [] |
| 53 | + self.numindices = {} |
| 54 | + |
| 55 | + def insert(self, val: int) -> bool: |
| 56 | + self.numlist.append(val) |
| 57 | + if val not in self.numindices: |
| 58 | + self.numindices[val] = set() |
| 59 | + self.numindices[val].add(len(self.numlist) - 1) |
| 60 | + |
| 61 | + return len(self.numindices[val]) == 1 |
| 62 | + |
| 63 | + def remove(self, val: int) -> bool: |
| 64 | + if val not in self.numindices: |
| 65 | + return False |
| 66 | + |
| 67 | + i = self.numindices[val].pop() |
| 68 | + if len(self.numindices[val]) == 0: |
| 69 | + self.numindices.pop(val) |
| 70 | + if i == len(self.numlist) - 1: |
| 71 | + self.numlist.pop() |
| 72 | + else: |
| 73 | + self.numlist[i] = self.numlist.pop() |
| 74 | + self.numindices[self.numlist[i]].remove(len(self.numlist)) |
| 75 | + self.numindices[self.numlist[i]].add(i) |
| 76 | + |
| 77 | + return True |
| 78 | + |
| 79 | + def getRandom(self) -> int: |
| 80 | + return random.choice(self.numlist) |
| 81 | + |
| 82 | + |
| 83 | +# Your RandomizedCollection object will be instantiated and called as such: |
| 84 | +# obj = RandomizedCollection() |
| 85 | +# param_1 = obj.insert(val) |
| 86 | +# param_2 = obj.remove(val) |
| 87 | +# param_3 = obj.getRandom() |
| 88 | +``` |
0 commit comments