Monday, November 13, 2017

Insert Delete GetRandom O(1)


https://leetcode.com/problems/insert-delete-getrandom-o1/description/
import java.util.Random;

public class RandomizedSet {

    /** Initialize your data structure here. */
    Map<Integer, Integer> numToIdx;
    Map<Integer, Integer> idxToNum;
    
    public RandomizedSet() {
        numToIdx = new HashMap<>();
        idxToNum = new HashMap<>();
    }
    
    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    public boolean insert(int val) {
        if (numToIdx.containsKey(val)) {
            return false;
        }
        
        int idxForInsertion = numToIdx.size();
        numToIdx.put(val, idxForInsertion);
        idxToNum.put(idxForInsertion, val);
        return true;
    }
    
    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        if (!numToIdx.containsKey(val)) {
            return false;
        }
        
        int idxOfVal = numToIdx.get(val);
        numToIdx.remove(val);
        idxToNum.remove(idxOfVal);
        
        if (numToIdx.size() == 0) {
            return true;
        }
        
        if (idxOfVal == numToIdx.size()) {
            return true;
        }
        
        // val was neither the only element, nor the last element.
        // after val was removed, we have to fill val's idx.
        // remove the last element in idxToNum and add it at idxOfVal instead.
        // the idx of the last element is idxToNum.size() as there were idxToNum.size() + 1 numbers
        // before val was removed and the last element had idx idxToNum.size()
        int numAtLastIdx = idxToNum.get(idxToNum.size());
        numToIdx.put(numAtLastIdx, idxOfVal);
        idxToNum.remove(idxToNum.size());
        idxToNum.put(idxOfVal, numAtLastIdx);
        return true;
    }
    
    /** Get a random element from the set. */
    public int getRandom() {
        if (numToIdx.size() == 0) {
            return -1;
        }
        
        if (numToIdx.size() == 1) {
            return idxToNum.get(0);
        }
        
        return idxToNum.get(new Random().nextInt(numToIdx.size()));
    }
}

/**
 * Your RandomizedSet object will be instantiated and called as such:
 * RandomizedSet obj = new RandomizedSet();
 * boolean param_1 = obj.insert(val);
 * boolean param_2 = obj.remove(val);
 * int param_3 = obj.getRandom();
 */

No comments:

Post a Comment