Skip to content

Commit ff69318

Browse files
edit 398
1 parent 636640c commit ff69318

File tree

3 files changed

+77
-77
lines changed

3 files changed

+77
-77
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ Your ideas/fixes/algorithms are more than welcome!
201201
|401|[Binary Watch](https://leetcode.com/problems/binary-watch/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_401.java)| O(1)|O(1) | Easy|
202202
|400|[Nth Digit](https://leetcode.com/problems/nth-digit/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_400.java)| O(n)|O(1) | Easy|
203203
|399|[Evaluate Division](https://leetcode.com/problems/evaluate-division/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_399.java)| O(n*n!)|O(n) | Medium| Graph, DFS, Backtracking
204-
|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/RandomPickIndex.java) | | | Medium| Reservoir Sampling
204+
|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_398.java) | | | Medium| Reservoir Sampling
205205
|397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/IntegerReplacement.java)| ? | ? | Easy| BFS
206206
|396|[Rotate Function](https://leetcode.com/problems/rotate-function/)|[Solution](../master/src/main/java/com/fishercoder/solutions/RotateFunction.java)| O(n^2) could be optimized to O(n) | O(1) | Easy|
207207
|393|[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_393.java)| O(?)|O(?) | Medium| Bit Manipulation

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

-76
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.
9+
10+
Note:
11+
The array size can be very large. Solution that uses too much extra space will not pass the judge.
12+
13+
Example:
14+
15+
int[] nums = new int[] {1,2,3,3,3};
16+
Solution solution = new Solution(nums);
17+
18+
// pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
19+
solution.pick(3);
20+
21+
// pick(1) should return 0. Since in the array only nums[0] is equal to 1.
22+
solution.pick(1);*/
23+
public class _398 {
24+
25+
//TODO: use reservoir sampling to solve it again
26+
27+
class Solution {
28+
//brute force
29+
int[] input;
30+
java.util.Random rand = new java.util.Random();
31+
32+
public Solution(int[] nums) {
33+
input = nums;
34+
}
35+
36+
public int pick(int target) {
37+
List<Integer> list = new ArrayList();
38+
for (int i = 0; i < input.length; i++) {
39+
if (input[i] == target) {
40+
list.add(i);
41+
}
42+
}
43+
if (list.size() == 1) return list.get(0);
44+
int randomIndex = rand.nextInt(list.size());
45+
return list.get(randomIndex);
46+
}
47+
}
48+
49+
50+
class Solution_MemoryLimitExceeded {
51+
52+
private Map<Integer, List<Integer>> map = new HashMap();
53+
java.util.Random rand = new java.util.Random();
54+
55+
public Solution_MemoryLimitExceeded(int[] nums) {
56+
for (int i = 0; i < nums.length; i++) {
57+
if (map.containsKey(nums[i])) {
58+
List<Integer> list = map.get(nums[i]);
59+
list.add(i);
60+
map.put(nums[i], list);
61+
} else {
62+
List<Integer> list = new ArrayList();
63+
list.add(i);
64+
map.put(nums[i], list);
65+
}
66+
}
67+
}
68+
69+
public int pick(int target) {
70+
List<Integer> list = map.get(target);
71+
if (list.size() == 1) return list.get(0);
72+
int randomIndex = rand.nextInt(list.size());
73+
return list.get(randomIndex);
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)