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.
Implement the RandomizedCollection
class:
RandomizedCollection()
Initializes the empty RandomizedCollection
object.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.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.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.You must implement the functions of the class such that each function works on average O(1)
time complexity.
Note: The test cases are generated such that getRandom
will only be called if there is at least one item in the RandomizedCollection
.
Example 1:
Input ["RandomizedCollection", "insert", "insert", "insert", "getRandom", "remove", "getRandom"] [[], [1], [1], [2], [], [1], []] Output [null, true, false, true, 2, true, 1] Explanation 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.
Constraints:
-231 <= val <= 231 - 1
2 * 105
calls in total will be made to insert
, remove
, and getRandom
.getRandom
is called.This problem requires designing a data structure that supports insertion, deletion, and random element retrieval in O(1) average time complexity, even with duplicate elements. The naive approach of using a simple array and hashmap wouldn't suffice because removing an element from an array takes O(n) time in the worst case.
The solution uses a combination of a hashmap and a list to achieve O(1) average time complexity for all operations.
Data Structures:
m
(HashMap): Maps each value (val
) to a set of indices in the list l
where that value is stored. This allows us to quickly find all occurrences of a specific value.l
(List): Stores the elements of the collection. The order of elements in l
is important for getRandom()
to maintain proper probability distribution.Operations:
insert(val)
:
val
exists in m
. If not, create a new set for it.len(l)
) to the set associated with val
.val
to the end of l
.True
if the set for val
had size 0 (meaning this is the first insertion of val
), False
otherwise.remove(val)
:
val
exists in m
. If not, return False
.val
from m
. Grab the first index from this set.l
. This is crucial for maintaining the O(1) average time complexity for removal and keeping the probability distribution correct for getRandom()
.m
for the moved last element, reflecting its new position.val
.l
.val
is now empty, remove val
from m
.True
.getRandom()
:
l
using random.choice()
(Python) or rnd.nextInt(size)
(Java). Since elements are stored in l
with a probability proportional to their frequency, random.choice
(or equivalent) ensures the correct probability distribution.Time Complexity Analysis:
insert(val)
: O(1) on average. Hashmap operations (get, put) are O(1) on average. List append is O(1) amortized.remove(val)
: O(1) on average. Hashmap operations are O(1) on average. List operations are O(1) amortized. The worst case is O(n) if you're consistently removing elements from the beginning, but in a random scenario this is rare.getRandom()
: O(1). Random element selection from a list takes O(1) time.Space Complexity: O(n), where n is the number of elements in the collection, due to the storage in both the hashmap and the list.
Code Examples (Python and Java):
The provided code snippets above in Python and Java demonstrate the implementation of this optimized solution. The comments within the code explain each step in detail. Remember to include import random
in Python. In Java, import java.util.*;
is needed.