leetcode-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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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();

分析


这题紧接上一题 380. Insert Delete GetRandom O(1),但要求允许插入重复值。由于允许插入重复值,哈希表的结构应该是 value-list 这样的对应关系,其中 list 记录的是数组中的索引值,插入,删除的逻辑大致一样,但我们需要有一个方法可以从数组中找到哈希表的列表中的某个值,因此需要记录哈希表中 list 的索引,因此引入 Element 数据结构,除了保存本身的值外,还记录哈希表中 list 的索引,这样就能找到数组和 Map 的对应关系了。

解决方案1(Java)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class RandomizedCollection {
private HashMap<Integer, List<Integer>> hashIndexListMap;
private ArrayList<Element> elementList;
private java.util.Random rand = new java.util.Random();
private int size;

/** Initialize your data structure here. */
public RandomizedCollection() {
this.elementList = new ArrayList<Element>();
this.hashIndexListMap = new HashMap<Integer, List<Integer>>();
this.size = 0;
}

/** 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> indexList = this.hashIndexListMap.getOrDefault(val, new ArrayList<>());
this.elementList.add(new Element(val, indexList.size()));
indexList.add(this.size);
this.hashIndexListMap.put(val, indexList);
this.size++;
return indexList.size() == 1;
}

/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
List<Integer> indexList = this.hashIndexListMap.get(val);
if (indexList == null) {
return false;
}
int elementIndex = indexList.get(indexList.size() - 1);
Element lastElement = this.elementList.get(this.elementList.size() - 1);

List<Integer> lastElementIndexList = this.hashIndexListMap.get(lastElement.value);
lastElementIndexList.set(lastElement.index, elementIndex);
this.elementList.set(elementIndex, lastElement);

this.elementList.remove(this.elementList.size() - 1);
indexList.remove(indexList.size() - 1);
if (indexList.isEmpty()) {
this.hashIndexListMap.remove(val);
}
this.size--;
return true;
}

/** Get a random element from the collection. */
public int getRandom() {
return this.elementList.get(rand.nextInt(this.elementList.size())).value;
}
}

class Element {
int value;
int index;

Element(int value, int index) {
this.value = value;
this.index = index;
}
}

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

相关问题


(M) Insert Delete GetRandom O(1)

题目来源