Insert Delete GetRandom O(1) - Duplicates allowed

Design a data structure that supports all following operations in average O(1) time.Note: Duplicate elements are allowed.

  1. insert(val): Inserts an item val to the collection.

  2. remove(val): Removes an item val from the collection if present.

  3. getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.

Example:

// Init an empty collection.
RandomizedCollection collection = new RandomizedCollection();

// Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1);

// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].
collection.insert(1);

// Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
collection.insert(2);

// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
collection.getRandom();

// Removes 1 from the collection, returns true. Collection now contains [1,2].
collection.remove(1);

// getRandom should return 1 and 2 both equally likely.
collection.getRandom();
class NumAndIndex{
    public int number;
    public int index;
    
    public NumAndIndex(int number,int index){
        this.number = number;
        this.index = index;
    }
}


class RandomizedCollection {
    private List<NumAndIndex> list;
    private Map<Integer, List<Integer>> map;
    /** Initialize your data structure here. */
    public RandomizedCollection() {
        this.list = new ArrayList<>();
        this.map = new HashMap<>();
    }
    
    /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
    public boolean insert(int val) {
        //!conatinsKey(val)
        boolean exist = map.containsKey(val);
        if(!exist){
            map.put(val,new ArrayList<Integer>());
        }
        
        List<Integer> indexList = map.get(val);
        
        //巧妙的利用了index,list为空时,加进去刚好在0的位置,list 大小为1时,再加进去 刚好在index 1的位置
        indexList.add(list.size());
        
        
        //用来记录这个jindex加到了map里 index list的哪个位置用于remove时更新
        list.add(new NumAndIndex(val,indexList.size()-1));
        
        return !exist;
        
    }
    
    /** Removes a value from the collection. Returns true if the collection contained the specified element. */
    public boolean remove(int val) {
        if(!map.containsKey(val)){
            return false;
        }
        
        List<Integer> indexList = map.get(val);
        
        //find last item in list
        NumAndIndex item = list.get(list.size()-1);
        
        //find index
        int index = indexList.get(indexList.size()-1);
        //repalce
        list.set(index,item);
        
        //remove last from list
        
        list.remove(list.size()-1);
        
        //根据item number里的number和index,找到对应number的list,根据index找到之前这个item存在了maplist的位置,
        //然后更新现在的index
        map.get(item.number).set(item.index,index);
        
        map.get(val).remove(indexList.size()-1);
        if(map.get(val).isEmpty()){
            map.remove(val);
        }
        
        return true;
        
    }
    
    /** Get a random element from the collection. */
    public int getRandom() {
        Random r = new Random();
        
        int index= r.nextInt(list.size());
        
        return list.get(index).number;
    }
}

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

Last updated