Advertisement

Insert Delete GetRandom O(1) - LeetCode 380 Solution

Insert Delete GetRandom O(1) - Complete Solution Guide

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

Problem Statement

Implement the RandomizedSet class: RandomizedSet() Initializes the RandomizedSet object. bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise. bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise. int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each ele

Detailed Explanation

The problem requires implementing a `RandomizedSet` data structure that supports three operations in O(1) average time complexity: inserting a value, removing a value, and retrieving a random value from the set. The `insert` operation should only add the value if it's not already present, returning `true` if inserted and `false` otherwise. The `remove` operation should only remove the value if it exists, returning `true` if removed and `false` otherwise. The `getRandom` operation should return a random element from the set, ensuring each element has an equal probability of being selected. The data structure must efficiently handle insertions, deletions, and random selection while maintaining O(1) average time complexity for all operations.

Solution Approach

The solution uses a combination of a hash map (dictionary) and a list (array). The hash map stores each value in the set as a key, with its corresponding index in the list as the value. The list stores all the values present in the set. `insert` checks if the value exists in the map; if not, it adds the value to the list and updates the map with the value and its index. `remove` checks if the value exists in the map; if so, it swaps the value to be removed with the last value in the list, updates the map, removes the last value from the list and removes the value from the map. `getRandom` generates a random index within the bounds of the list and returns the value at that index.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash map (dictionary) and a list (array) in the constructor of the `RandomizedSet` class.
  2. Step 2: In the `insert` method, check if the value is already present in the hash map. If it is, return `false`.
  3. Step 3: If the value is not present, add it to the list. Then, store the value and its index in the list into the hash map. Return `true`.
  4. Step 4: In the `remove` method, check if the value is present in the hash map. If it isn't, return `false`.
  5. Step 5: If the value is present, get its index from the hash map.
  6. Step 6: Get the last element from the list.
  7. Step 7: Replace the element to be removed with the last element.
  8. Step 8: Update the hash map with the new index of the last element.
  9. Step 9: Remove the last element from the list.
  10. Step 10: Remove the value from the hash map. Return `true`.
  11. Step 11: In the `getRandom` method, generate a random index within the bounds of the list's length.
  12. Step 12: Return the element at the random index from the list.

Key Insights

  • Insight 1: Using a hash map (dictionary) allows for O(1) average time complexity for `insert` and `remove` operations by providing quick lookups to check for the presence of an element and its index.
  • Insight 2: Maintaining a list (array) allows for O(1) average time complexity for the `getRandom` operation since we can randomly access elements by index. However, removing an element from the middle of a list is typically O(n).
  • Insight 3: The key to achieving O(1) average time complexity for `remove` is to swap the element to be removed with the last element in the list and then pop the last element. This maintains the ability to randomly access elements in O(1) while avoiding shifting elements in the array.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(n)

Topics

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

Companies

Asked at: Affirm, Agoda, AppFolio, Axon, ByteDance, Cisco, Citadel, DE Shaw, Docusign, DoorDash, Goldman Sachs, Grammarly, Groupon, IXL, Intuit, LinkedIn, MakeMyTrip, Miro, Netflix, Nvidia, Okta, Palo Alto Networks, Peloton, Pocket Gems, Pure Storage, Quora, Rippling, Rubrik, Salesforce, Samsung, Snap, Snowflake, SoFi, Sprinklr, ThousandEyes, Unity, X, Yandex, Yelp, Zeta.