Skip to content

Commit f10b4d2

Browse files
edit 380
1 parent 482edc6 commit f10b4d2

File tree

3 files changed

+107
-131
lines changed

3 files changed

+107
-131
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ Your ideas/fixes/algorithms are more than welcome!
231231
|383|[Ransom Note](https://leetcode.com/problems/ransom-note/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_383.java)| O(n)|O(n) | Medium|
232232
|382|[Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_382.java)| O(1)|O(n) | Medium| Reservoir Sampling
233233
|381|[Insert Delete GetRandom O(1) - Duplicates allowed](https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_381.java)| | | Hard|
234+
|380|[Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_380.java)| O(n) | O(1)| Medium| Design, HashMap
234235
|379|[Design Phone Directory](https://leetcode.com/problems/design-phone-directory/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_379.java)| O(1)|O(n) | Medium|
235236
|377|[Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_377.java)| O(?)|O(?) | Medium|
236237
|376|[Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_376.java)| O(n)|O(1) | Medium| DP, Greedy

src/main/java/com/fishercoder/solutions/RandomizedSet.java

Lines changed: 0 additions & 131 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Random;
6+
7+
/**
8+
* 380. Insert Delete GetRandom O(1)
9+
* Design a data structure that supports all following operations in average O(1) time.
10+
* <p>
11+
* insert(val): Inserts an item val to the set if not already present.
12+
* remove(val): Removes an item val from the set if present.
13+
* getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.
14+
* Example:
15+
* <p>
16+
* // Init an empty set.
17+
* RandomizedSet randomSet = new RandomizedSet();
18+
* <p>
19+
* // Inserts 1 to the set. Returns true as 1 was inserted successfully.
20+
* randomSet.insert(1);
21+
* <p>
22+
* // Returns false as 2 does not exist in the set.
23+
* randomSet.remove(2);
24+
* <p>
25+
* // Inserts 2 to the set, returns true. Set now contains [1,2].
26+
* randomSet.insert(2);
27+
* <p>
28+
* // getRandom should return either 1 or 2 randomly.
29+
* randomSet.getRandom();
30+
* <p>
31+
* // Removes 1 from the set, returns true. Set now contains [2].
32+
* randomSet.remove(1);
33+
* <p>
34+
* // 2 was already in the set, so return false.
35+
* randomSet.insert(2);
36+
* <p>
37+
* // Since 2 is the only number in the set, getRandom always return 2.
38+
* randomSet.getRandom();
39+
*/
40+
41+
public class _380 {
42+
/**
43+
* Your _380 object will be instantiated and called as such:
44+
* _380 obj = new _380();
45+
* boolean param_1 = obj.insert(val);
46+
* boolean param_2 = obj.delete(val);
47+
* int param_3 = obj.getRandom();
48+
*/
49+
50+
public static class RandomizedSet {
51+
52+
Map<Integer, Integer> forwardMap;//key is auto increment index, value if the inserted val
53+
Map<Integer, Integer> reverseMap;//the other way around
54+
int index;
55+
Random random;
56+
57+
/**
58+
* Initialize your data structure here.
59+
*/
60+
public RandomizedSet() {
61+
forwardMap = new HashMap();
62+
reverseMap = new HashMap();
63+
index = 0;
64+
random = new Random();
65+
}
66+
67+
/**
68+
* Inserts a value to the set. Returns true if the set did not already contain the specified element.
69+
*/
70+
public boolean insert(int val) {
71+
if (forwardMap.containsValue(val)) return false;
72+
else {
73+
forwardMap.put(index, val);
74+
reverseMap.put(val, index++);
75+
return true;
76+
}
77+
}
78+
79+
/**
80+
* Deletes a value from the set. Returns true if the set contained the specified element.
81+
*/
82+
public boolean remove(int val) {
83+
if (forwardMap.containsValue(val)) {
84+
int key = reverseMap.get(val);
85+
reverseMap.remove(val);
86+
forwardMap.remove(key);
87+
return true;
88+
} else {
89+
return false;
90+
}
91+
}
92+
93+
/**
94+
* Get a random element from the set.
95+
*/
96+
public int getRandom() {
97+
int max = forwardMap.size();
98+
if (max == 1) return forwardMap.get(index - 1);
99+
int randomNum = random.nextInt(max);
100+
while (!forwardMap.containsKey(randomNum)) {
101+
randomNum = random.nextInt(max);
102+
}
103+
return forwardMap.get(randomNum);
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)