Design a data structure that supports all following operations in average O(1) time.
Note: Duplicate elements are allowed.
-
insert(val)
: Inserts an item val to the collection. -
remove(val)
: Removes an item val from the collection if present. -
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();
O(1) 时间插入、删除和获取随机元素 - 允许重复。题意跟380题很接近,唯一不同的条件是这个题允许加入相同元素。
思路还是用hashmap + arrayList,hashmap的key是每个不同的数字,value是一个hashset,存的是每个数字在list里的index。
insert() - 遇到相同的数字的时候,每次记得先在hashmap的key中加入list当前的size再处理list的部分。arrayList还是保存每一个被加入的num。
remove() - 从hashmap里删除的时候,如果某个元素出现过多次,则删除一次就好,此时需要对hashset做iterate;如果这个元素只出现了一次,记得要将hashmap中的key也一并删除。对list做删除操作的时候,跟380题一样,也是需要把list的最后一个元素移动到list里被删除的元素的位置上。
时间O(1) - required
空间O(n)
Java实现
1 class RandomizedCollection { 2 3 HashMap<Integer, HashSet<Integer>> map; 4 List<Integer> list; 5 Random rmd; 6 7 /** Initialize your data structure here. */ 8 public RandomizedCollection() { 9 map = new HashMap<>(); 10 list = new ArrayList<>(); 11 rmd = new Random(); 12 } 13 14 /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */ 15 public boolean insert(int val) { 16 boolean contain = map.containsKey(val); 17 if (!contain) { 18 map.put(val, new HashSet<>()); 19 } 20 map.get(val).add(list.size()); 21 list.add(val); 22 return !contain; 23 } 24 25 /** Removes a value from the collection. Returns true if the collection contained the specified element. */ 26 public boolean remove(int val) { 27 if (!map.containsKey(val)) { 28 return false; 29 } 30 int index = map.get(val).iterator().next(); 31 map.get(val).remove(index); 32 if (map.get(val).size() == 0) { 33 map.remove(val); 34 } 35 int lastVal = list.remove(list.size() - 1); 36 if (index != list.size()) { 37 list.set(index, lastVal); 38 map.get(lastVal).remove(list.size()); 39 map.get(lastVal).add(index); 40 } 41 return true; 42 } 43 44 /** Get a random element from the collection. */ 45 public int getRandom() { 46 return list.get(rmd.nextInt(list.size())); 47 } 48 } 49 50 /** 51 * Your RandomizedCollection object will be instantiated and called as such: 52 * RandomizedCollection obj = new RandomizedCollection(); 53 * boolean param_1 = obj.insert(val); 54 * boolean param_2 = obj.remove(val); 55 * int param_3 = obj.getRandom(); 56 */
相关题目
380. Insert Delete GetRandom O(1)
381. Insert Delete GetRandom O(1) - Duplicates allowed