java.lang.Object
g0301_0400.s0381_insert_delete_getrandom_o1_duplicates_allowed.RandomizedCollection

public class RandomizedCollection extends Object
381 - Insert Delete GetRandom O(1) - Duplicates allowed.<p>Hard</p> <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 removing a random element.</p> <p>Implement the <code>RandomizedCollection</code> class:</p> <ul> <li><code>RandomizedCollection()</code> Initializes the empty <code>RandomizedCollection</code> object.</li> <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> <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> <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 same values the multiset contains.</li> </ul> <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> <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> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <pre><code> [&quot;RandomizedCollection&quot;, &quot;insert&quot;, &quot;insert&quot;, &quot;insert&quot;, &quot;getRandom&quot;, &quot;remove&quot;, &quot;getRandom&quot;] [ [], [1], [1], [2], [], [1], []] </code></pre> <p><strong>Output:</strong> [null, true, false, true, 2, true, 1]</p> <p><strong>Explanation:</strong></p> <pre><code> RandomizedCollection randomizedCollection = new RandomizedCollection(); randomizedCollection.insert(1); // return true since the collection does not contain 1. // Inserts 1 into the collection. randomizedCollection.insert(1); // return false since the collection contains 1. // Inserts another 1 into the collection. Collection now contains [1,1]. randomizedCollection.insert(2); // return true since the collection does not contain 2. // Inserts 2 into the collection. Collection now contains [1,1,2]. randomizedCollection.getRandom(); // getRandom should: // - return 1 with probability 2/3, or // - return 2 with probability 1/3. randomizedCollection.remove(1); // return true since the collection contains 1. // Removes 1 from the collection. Collection now contains [1,2]. randomizedCollection.getRandom(); // getRandom should return 1 or 2, both equally likely. </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> <= val <= 2<sup>31</sup> - 1</code></li> <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> <li>There will be <strong>at least one</strong> element in the data structure when <code>getRandom</code> is called.</li> </ul>
  • Constructor Details

    • RandomizedCollection

      public RandomizedCollection()
  • Method Details

    • insert

      public boolean insert(int val)
    • remove

      public boolean remove(int val)
    • getRandom

      public int getRandom()