forked from xinw3/leetcode-coding-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomizedCollection.java
More file actions
100 lines (89 loc) · 3.03 KB
/
RandomizedCollection.java
File metadata and controls
100 lines (89 loc) · 3.03 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class RandomizedCollection {
Map<Integer, Set<Integer>> randomMap;
List<Integer> randomList;
Set<Integer> indexSet;
int index;
Random generator;
/** Initialize your data structure here. */
public RandomizedCollection() {
randomMap = new HashMap<Integer, Set<Integer>>();
randomList = new ArrayList<Integer>();
generator = 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 (!randomMap.containsKey(val)) {
indexSet = new LinkedHashSet<Integer>();
indexSet.add(randomList.size());
randomMap.put(val, indexSet);
randomList.add(val);
return true;
}
indexSet = randomMap.get(val);
indexSet.add(randomList.size());
randomMap.put(val, indexSet);
randomList.add(val);
return false;
}
/** Removes a value from the collection. Returns true if the collection contained the specified element. */
public boolean remove(int val) {
if (randomMap.containsKey(val)) {
indexSet = randomMap.get(val);
index = indexSet.iterator().next();
indexSet.remove(index);
//randomMap.put(val, indexList);
if (index < randomList.size() - 1) {
int last = randomList.get(randomList.size() - 1);
randomList.set(index, last);
Set<Integer> lastSet = randomMap.get(last);
lastSet.remove(randomList.size() - 1);
lastSet.add(index);
}
randomList.remove(randomList.size() - 1);
if (indexSet.isEmpty()) {
indexSet.remove(val);
}
return true;
}
return false;
}
/** Get a random element from the collection. */
public int getRandom() {
if (randomList.size() != 0) {
index = generator.nextInt(randomList.size());
return randomList.get(index);
}
return 0;
}
/**
* 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();
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RandomizedCollection obj = new RandomizedCollection();
obj.insert(9);
obj.insert(9);
obj.insert(1);
obj.insert(1);
obj.insert(2);
obj.insert(1);
obj.remove(2);
obj.remove(1);
obj.remove(1);
obj.insert(9);
obj.remove(1);
//int param_3 = obj.getRandom();
}
}