Advertisement

Insert Delete GetRandom O(1) - Duplicates allowed - LeetCode 381 Solution

Insert Delete GetRandom O(1) - Duplicates allowed - Complete Solution Guide

Insert Delete GetRandom O(1) - Duplicates allowed is LeetCode problem 381, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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(i

Detailed Explanation

The problem requires designing a data structure, `RandomizedCollection`, that supports insertion, removal, and random element retrieval in (average) O(1) time, even when duplicate values are present. The `insert` method should return `true` only if the value wasn't present *at all* in the collection, and `false` otherwise. The `remove` method should remove only *one* instance of the value, and return `true` if the value was found and removed, `false` otherwise. `getRandom` should return a random element from the collection, where each element's probability of being chosen is proportional to its frequency in the collection.

Solution Approach

The solution uses a list (or array) `nums` to store the elements of the collection and a hash map `idx_map` to store the mapping between each value and the set of indices where it appears in `nums`. The `insert` operation appends the new element to `nums` and updates `idx_map`. The `remove` operation finds one index of the element to be removed, swaps the element at that index with the last element in `nums`, removes the last element, and updates `idx_map` accordingly. `getRandom` returns a random element from `nums`.

Step-by-Step Algorithm

  1. Step 1: **Initialize**: Create a list `nums` to store the values and a hash map `idx_map` to store the mapping between value and a set of indices.
  2. Step 2: **Insert**: Append `val` to `nums`. Update `idx_map` by adding the index of the newly inserted `val` to the set associated with `val`. Return `true` if `val` was not previously in `idx_map`, otherwise return `false`.
  3. Step 3: **Remove**: If `val` is not in `idx_map`, return `false`. Otherwise, get one index of `val` from `idx_map`. If the index is not the last index, swap `val` at `idx_to_remove` with the last element of nums. Update the idx_map. Finally, remove the last element. If set associated with val is empty, delete the val entry from idx_map.
  4. Step 4: **getRandom**: Generate a random index within the range of `nums` and return the element at that index.

Key Insights

  • Insight 1: Using a list (or array) allows for O(1) random element selection using an index.
  • Insight 2: A hash map can be used to store the values and their corresponding indices in the list, enabling O(1) lookup and removal.
  • Insight 3: Removing an element from the middle of an array takes O(n) time. Swapping the element to be removed with the last element and then popping the last element allows for O(1) removal on average.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Math, Design, Randomized.

Companies

Asked at: Affirm, Citadel, LinkedIn, Peloton, Yelp, Zeta.