Similar Problems

Similar Problems not available

Insert Delete Getrandom O1 - Leetcode Solution

LeetCode:  Insert Delete Getrandom O1 Leetcode Solution

Difficulty: Medium

Topics: math design array hash-table  

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 element must have the same probability of being returned.

Solution: The key to solving this problem is to store elements in a vector and use a hash table to keep track of the index of each element in the vector. The insert operation can be done in O(1) time by adding the element to the end of the vector and updating the hash table to map the element to its index in the vector. The remove operation requires us to remove an element from the vector, but removing an element from the middle of a vector requires shifting all the elements to the right of the element being removed by one position, which takes O(n) time. To avoid this, we can swap the element to be removed with the last element in the vector, and then remove the last element from the vector. This only takes O(1) time. We also need to update the hash table to map the index of the last element to the index of the removed element.

To get a random element, we generate a random index i between 0 and the size of the vector - 1, and return the element at that index in the vector. This takes O(1) time.

Here's the implementation:

class RandomizedSet {
public:
    unordered_map<int, int> indices;
    vector<int> elements;

    /** Initialize your data structure here. */
    RandomizedSet() {
        
    }
    
    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    bool insert(int val) {
        if (indices.count(val)) return false;
        elements.push_back(val);
        indices[val] = elements.size() - 1;
        return true;
    }
    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    bool remove(int val) {
        if (!indices.count(val)) return false;
        int index = indices[val];
        indices[elements.back()] = index; // update the index of the last element to the index of the removed element
        elements[index] = elements.back(); // swap the element to be removed with the last element
        elements.pop_back(); // remove the last element
        indices.erase(val); // remove the index of the removed element
        return true;
    }
    
    /** Get a random element from the set. */
    int getRandom() {
        int index = rand() % elements.size();
        return elements[index];
    }
};

Time Complexity:

  • Insertion and removal of elements take O(1) time on average due to the hash table.
  • Getting a random element takes O(1) time.
  • Overall time complexity is O(1) for all operations.

Insert Delete Getrandom O1 Solution Code

1