
Problem
Solution
Submissions
Insert Delete GetRandom O(1)
Certification: Advanced Level
Accuracy: 100%
Submissions: 1
Points: 15
Design a data structure that supports all following operations in average O(1) time.
insert(val)
: Inserts an item val into the collection.remove(val)
: Removes an item val from the collection if present.getRandom()
: Returns a random element from the current collection of elements. Each element must have the same probability of being returned.
Note that duplicates are allowed, which means you need to handle multiple copies of the same value in the collection.
Example 1
- // Initialize an empty collection.
RandomizedCollection collection = new RandomizedCollection(); - // Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1); // return true - // Inserts another 1 to the collection. Returns false as the collection contained 1.
// Collection now contains [1,1].
collection.insert(1); // return false - // Inserts 2 to the collection. Returns true as the collection did not contain 2.
// Collection now contains [1,1,2].
collection.insert(2); // return true - // getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
collection.getRandom(); - // Removes 1 from the collection, returns true. Collection now contains [1,2].
collection.remove(1); // return true - // getRandom should return 1 and 2 both with a probability of 1/2.
collection.getRandom(); - // Removes 2 from the collection, returns true. Collection now contains [1].
collection.remove(2); // return true - // getRandom should return 1 with probability 1.
collection.getRandom();
Example 2
- // Initialize an empty collection.
RandomizedCollection collection = new RandomizedCollection(); - // Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1); // return true - // Inserts 2 to the collection. Returns true as the collection did not contain 2.
// Collection now contains [1,2].
collection.insert(2); // return true - // getRandom should return 1 and 2 both with a probability of 1/2.
collection.getRandom(); - // Removes 1 from the collection, returns true. Collection now contains [2].
collection.remove(1); // return true - // Inserts 3 to the collection. Returns true as the collection did not contain 3.
// Collection now contains [2,3].
collection.insert(3); // return true - // getRandom should return 2 and 3 both with a probability of 1/2.
collection.getRandom();
Constraints
- -2^31 <= val <= 2^31 - 1
- At most 2 * 10^5 calls will be made to insert, remove, and getRandom.
- There will be at least one element in the data structure when getRandom is called.
- Time Complexity for all operations should be O(1) on average
- Space Complexity: O(n) where n is the number of elements
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use an ArrayList to store elements for O(1) access by index.
- Use a HashMap to map values to their indices in the ArrayList for O(1) lookup.
- Since duplicates are allowed, each value in the HashMap needs to map to a set of indices.
- When removing, swap the element to be removed with the last element in the ArrayList.
- When getting a random element, use a random index in the ArrayList.