381. Insert Delete GetRandom O(1) - Duplicates allowed

381. 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();









https://www.youtube.com/watch?v=mRTgft9sBhA

这个自己写写,花花讲的思路还行

https://zxi.mytechroad.com/blog/hashtable/leetcode-381-insert-delete-getrandom-o1-duplicates-allowed/

// correct

class RandomizedCollection {
  class Entry {
    public int value;
    public int index;
    public Entry(int val, int idx) {
      value = val;
      index = idx;
    }
  }
  
  private Map<Integer, List<Integer>> m;
  private List<Entry> vals;
  private Random rand;
 
  /** Initialize your data structure here. */
  public RandomizedCollection() {
    m = new HashMap<>();
    vals = new ArrayList<>();
    rand = new Random();
  }
 
  /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
  public boolean insert(int val) {    
    List<Integer> l = m.getOrDefault(val, new ArrayList<Integer>());
    l.add(vals.size());
    m.put(val, l);
    vals.add(new Entry(val, l.size() - 1));
    return l.size() == 1;
  }
 
  /** Removes a value from the collection. Returns true if the collection contained the specified element. */
  public boolean remove(int val) {    
    if (!m.containsKey(val)) return false;
    List<Integer> l = m.get(val);
    int index_to_evict = l.get(l.size() - 1);
    Entry last_entry = vals.get(vals.size() - 1);
 
    // Update index
    m.get(last_entry.value).set(last_entry.index, index_to_evict);
 
    // Swap vals
    vals.set(index_to_evict, last_entry);    
 
    // Cleanup
    vals.remove(vals.size() - 1);
    l.remove(l.size() - 1);
    if (l.size() == 0) m.remove(val);
 
    return true;
  }
 
  /** Get a random element from the collection. */
  public int getRandom() {    
    return vals.get(rand.nextInt(vals.size())).value;
  }
}





没通过

class RandomizedCollection {

    /** Initialize your data structure here. */
  
    class Entry{
      public int value;
      public int index;
      public Entry(int val, int idx){
        value = val;
        index = idx;
      }
    }
  
  
    private  HashMap<Integer, List<Integer>> map;
    private List<Entry> vals;
    private Random rand;
  
  
    public RandomizedCollection() {
      map = new HashMap<>();
      vals = new ArrayList<>();
      rand = new Random();
        
    }
    
    /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
    public boolean insert(int val) {
      if(!map.containsKey(val)){
        map.put(val, new ArrayList<>());
      }
      List<Integer> list = map.get(val);
      list.add(vals.size());
      map.put(val, list);
      vals.add(new Entry(val, list.size()-1));
      return list.size() == 1;
        
    }
    
    /** 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;
      
      // need to get the index of the removing value in the list<entry>
      List<Integer> list = map.get(val);
      int size = list.size();
      int lastIndex = list.get(size -1);
      list.remove(size-1);
      if(list.size() == 0){
        map.remove(val);
      }
      
      // get the value from the last slot in the list<entry>
      int listSize = vals.size();
      Entry lastSlot = vals.get(listSize - 1);
      int lvalue = lastSlot.value;
      int lindex = lastSlot.index;
      
      map.get(lvalue).set(lindex,lastIndex);
      
      // swap the removing slot with the last slot 
      vals.set(lastIndex, lastSlot);
      vals.remove(listSize - 1);
      return true;
      
    }
    
    /** Get a random element from the collection. */
    public int getRandom() {
      return vals.get(rand.nextInt(vals.size())).value;
        
    }
}  
      

 

posted on 2018-08-10 14:44  猪猪&#128055;  阅读(143)  评论(0)    收藏  举报

导航