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

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     HashMap<Integer, HashSet<Integer>> map;
 3     List<Integer> list;
 4     Random rmd;
 5 
 6     /** Initialize your data structure here. */
 7     public RandomizedCollection() {
 8         map = new HashMap<>();
 9         list = new ArrayList<>();
10         rmd = new Random();
11     }
12     
13     /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
14     public boolean insert(int val) {
15         boolean contain = map.containsKey(val);
16         if (!contain) {
17             map.put(val, new HashSet<>());
18         }
19         map.get(val).add(list.size());
20         list.add(val);
21         return !contain;
22     }
23     
24     /** Removes a value from the collection. Returns true if the collection contained the specified element. */
25     public boolean remove(int val) {
26         if (!map.containsKey(val)) {
27             return false;
28         }
29         int firstIndex = map.get(val).iterator().next();
30         map.get(val).remove(firstIndex);
31         if (map.get(val).size() == 0) {
32             map.remove(val);
33         }
34         int lastVal = list.remove(list.size() - 1);
35         if (firstIndex != list.size()) {
36             list.set(firstIndex, lastVal);
37             map.get(lastVal).remove(list.size());
38             map.get(lastVal).add(firstIndex);
39         }
40         return true;
41     }
42     
43     /** Get a random element from the collection. */
44     public int getRandom() {
45         return list.get(rmd.nextInt(list.size()));
46     }
47 }
48 
49 /**
50  * Your RandomizedCollection object will be instantiated and called as such:
51  * RandomizedCollection obj = new RandomizedCollection();
52  * boolean param_1 = obj.insert(val);
53  * boolean param_2 = obj.remove(val);
54  * int param_3 = obj.getRandom();
55  */

 

相关题目

380. Insert Delete GetRandom O(1)

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

LeetCode 题目总结