Insert Delete GetRandom O(1) - Duplicates allowed — LeetCode 381 Python Solution
- Problem
- #381
- Pattern
- Math and Number Theory
- Reading time
- 9 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- ["RandomizedCollection", "insert", "insert", "insert", "getRandom", "remove", "getRandom"]
- Output
- [null, true, false, true, 2, true, 1]
- Explanation
- RandomizedCollection randomizedCollection = new RandomizedCollection();
Python solution
class RandomizedCollection:
def __init__(self):
"""
Initialize your data structure here.
"""
self.m = {}
self.l = []
def insert(self, val: int) -> bool:
"""
Inserts a value to the collection. Returns true if the collection did not already contain the specified element.
"""
idx_set = self.m.get(val, set())
idx_set.add(len(self.l))
self.m[val] = idx_set
self.l.append(val)
return len(idx_set) == 1
def remove(self, val: int) -> bool:
"""
Removes a value from the collection. Returns true if the collection contained the specified element.
"""
if val not in self.m:
return False
idx_set = self.m[val]
idx = list(idx_set)[0]
last_idx = len(self.l) - 1
self.l[idx] = self.l[last_idx]
idx_set.remove(idx)
last_idx_set = self.m[self.l[last_idx]]
if last_idx in last_idx_set:
last_idx_set.remove(last_idx)
if idx < last_idx:
last_idx_set.add(idx)
if not idx_set:
self.m.pop(val)
self.l.pop()
return True
def getRandom(self) -> int:
"""
Get a random element from the collection.
"""
return -1 if len(self.l) == 0 else random.choice(self.l)
# Your RandomizedCollection object will be instantiated and called as such:
# obj = RandomizedCollection()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed is filed here because LeetCode tags it Math, which is the vocabulary this hub collects.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed?
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed is rated Hard on LeetCode.
- What is the time complexity of LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed?
- The Python solution on this page uses O(n) auxiliary space.
- What topics does LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed cover?
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed is tagged Design, Array, Hash Table, Math and Randomized on LeetCode.