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.

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

解法1:

自己写的怎么都没过OA,这个AC的答案是网上抄的[捂脸]。主要的思想和没有duplicates的版本很相似。区别就是这里对于每一个数字,要存储的是出现的index的list。删除操作的时候,把末尾的元素直接写到被删除的元素的位置。同时维护一下两个index的list.
对于需要删除的数字,去掉list中相应的位置
对于末尾的数字,在list中加入要删除的数字的位置
在队列中把要删除的元素的位置置换为末尾的元素
删除队列末尾的元素(因为已经放到了队列中间去了)
C++

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
69
70
71
public class RandomizedCollection {
ArrayList<Integer> result;
HashMap<Integer, LinkedHashSet<Integer>> map;
public RandomizedCollection() {
result = new ArrayList<Integer>();
map = new HashMap<Integer, LinkedHashSet<Integer>>();
}
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
public boolean insert(int val) {
// Add item to map if it doesn't already exist.
boolean alreadyExists = map.containsKey(val);
if(!alreadyExists) {
map.put(val, new LinkedHashSet<Integer>());
}
map.get(val).add(result.size());
result.add(val);
return !alreadyExists;
}
/** 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;
}
// Get arbitary index of the ArrayList that contains val
LinkedHashSet<Integer> valSet = map.get(val);
int indexToReplace = valSet.iterator().next();
// Obtain the set of the number in the last place of the ArrayList
int numAtLastPlace = result.get(result.size() - 1);
LinkedHashSet<Integer> replaceWith = map.get(numAtLastPlace);
// Replace val at arbitary index with very last number
result.set(indexToReplace, numAtLastPlace);
// Remove appropriate index
valSet.remove(indexToReplace);
// Don't change set if we were replacing the removed item with the same number
if(indexToReplace != result.size() - 1) {
replaceWith.remove(result.size() - 1);
replaceWith.add(indexToReplace);
}
result.remove(result.size() - 1);
// Remove map entry if set is now empty, then return
if(valSet.isEmpty()) {
map.remove(val);
}
return true;
}
/** Get a random element from the collection. */
public int getRandom() {
// Get linearly random item
Random random = new Random();
return result.get(random.nextInt(result.size()));
// return result.get((int)(Math.random() * result.size()));
}
}
/**
* 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();
*/