Skip to content

Commit 0eeb592

Browse files
add 528
1 parent a6370a6 commit 0eeb592

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ _If you like this project, please leave me a star._ ★
820820
|531|[Lonely Pixel I](https://leetcode.com/problems/lonely-pixel-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_531.java) | |Medium |
821821
|530|[Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_530.java) | |Easy| DFS
822822
|529|[Minesweeper](https://leetcode.com/problems/minesweeper/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_529.java) | |Medium | BFS
823+
|528|[Random Pick with Weight](https://leetcode.com/problems/random-pick-with-weight/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_528.java) | |Medium | Math, Binary Search, Prefix Sum, Randomized
823824
|527|[Word Abbreviation](https://leetcode.com/problems/word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_527.java) | |Hard |
824825
|526|[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_526.java) | |Medium | Backtracking
825826
|525|[Contiguous Array](https://leetcode.com/problems/contiguous-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_525.java) | |Medium | HashMap
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Random;
4+
5+
public class _528 {
6+
public static class Solution1 {
7+
Random random;
8+
int[] preSums;
9+
10+
public Solution1(int[] w) {
11+
this.random = new Random();
12+
for (int i = 1; i < w.length; ++i) {
13+
w[i] += w[i - 1];
14+
}
15+
this.preSums = w;
16+
}
17+
18+
public int pickIndex() {
19+
int len = preSums.length;
20+
int idx = random.nextInt(preSums[len - 1]) + 1;
21+
int left = 0, right = len - 1;
22+
// search position
23+
while (left < right) {
24+
int mid = left + (right - left) / 2;
25+
if (preSums[mid] == idx)
26+
return mid;
27+
else if (preSums[mid] < idx)
28+
left = mid + 1;
29+
else
30+
right = mid;
31+
}
32+
return left;
33+
}
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._528;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
public class _528Test {
9+
private static _528.Solution1 solution1;
10+
private static int expected;
11+
12+
@Test
13+
public void test1() {
14+
solution1 = new _528.Solution1(new int[]{1});
15+
expected = 0;
16+
assertEquals(expected, solution1.pickIndex());
17+
}
18+
19+
@Test
20+
public void test2() {
21+
solution1 = new _528.Solution1(new int[]{1, 3});
22+
System.out.println(solution1.pickIndex());
23+
System.out.println(solution1.pickIndex());
24+
System.out.println(solution1.pickIndex());
25+
System.out.println(solution1.pickIndex());
26+
System.out.println(solution1.pickIndex());
27+
}
28+
29+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy