|
| 1 | +<h2><a href="https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed">Insert Delete GetRandom O(1) - Duplicates allowed</a></h2> <img src='https://img.shields.io/badge/Difficulty-Hard-red' alt='Difficulty: Hard' /><hr><p><code>RandomizedCollection</code> 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.</p> |
| 2 | + |
| 3 | +<p>Implement the <code>RandomizedCollection</code> class:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>RandomizedCollection()</code> Initializes the empty <code>RandomizedCollection</code> object.</li> |
| 7 | + <li><code>bool insert(int val)</code> Inserts an item <code>val</code> into the multiset, even if the item is already present. Returns <code>true</code> if the item is not present, <code>false</code> otherwise.</li> |
| 8 | + <li><code>bool remove(int val)</code> Removes an item <code>val</code> from the multiset if present. Returns <code>true</code> if the item is present, <code>false</code> otherwise. Note that if <code>val</code> has multiple occurrences in the multiset, we only remove one of them.</li> |
| 9 | + <li><code>int getRandom()</code> Returns a random element from the current multiset of elements. The probability of each element being returned is <strong>linearly related</strong> to the number of the same values the multiset contains.</li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>You must implement the functions of the class such that each function works on <strong>average</strong> <code>O(1)</code> time complexity.</p> |
| 13 | + |
| 14 | +<p><strong>Note:</strong> The test cases are generated such that <code>getRandom</code> will only be called if there is <strong>at least one</strong> item in the <code>RandomizedCollection</code>.</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong></p> |
| 18 | + |
| 19 | +<pre> |
| 20 | +<strong>Input</strong> |
| 21 | +["RandomizedCollection", "insert", "insert", "insert", "getRandom", "remove", "getRandom"] |
| 22 | +[[], [1], [1], [2], [], [1], []] |
| 23 | +<strong>Output</strong> |
| 24 | +[null, true, false, true, 2, true, 1] |
| 25 | + |
| 26 | +<strong>Explanation</strong> |
| 27 | +RandomizedCollection randomizedCollection = new RandomizedCollection(); |
| 28 | +randomizedCollection.insert(1); // return true since the collection does not contain 1. |
| 29 | + // Inserts 1 into the collection. |
| 30 | +randomizedCollection.insert(1); // return false since the collection contains 1. |
| 31 | + // Inserts another 1 into the collection. Collection now contains [1,1]. |
| 32 | +randomizedCollection.insert(2); // return true since the collection does not contain 2. |
| 33 | + // Inserts 2 into the collection. Collection now contains [1,1,2]. |
| 34 | +randomizedCollection.getRandom(); // getRandom should: |
| 35 | + // - return 1 with probability 2/3, or |
| 36 | + // - return 2 with probability 1/3. |
| 37 | +randomizedCollection.remove(1); // return true since the collection contains 1. |
| 38 | + // Removes 1 from the collection. Collection now contains [1,2]. |
| 39 | +randomizedCollection.getRandom(); // getRandom should return 1 or 2, both equally likely. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p> </p> |
| 43 | +<p><strong>Constraints:</strong></p> |
| 44 | + |
| 45 | +<ul> |
| 46 | + <li><code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code></li> |
| 47 | + <li>At most <code>2 * 10<sup>5</sup></code> calls <strong>in total</strong> will be made to <code>insert</code>, <code>remove</code>, and <code>getRandom</code>.</li> |
| 48 | + <li>There will be <strong>at least one</strong> element in the data structure when <code>getRandom</code> is called.</li> |
| 49 | +</ul> |
0 commit comments