Skip to content

Commit 8022ad3

Browse files
committed
完成710
1 parent 2fcc5b5 commit 8022ad3

File tree

6 files changed

+66
-8
lines changed

6 files changed

+66
-8
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4988,14 +4988,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
49884988
//|{doc_base_url}/0709-to-lower-case.adoc[题解]
49894989
//|Easy
49904990
//|
4991-
//
4992-
//|710
4993-
//|{leetcode_base_url}/random-pick-with-blacklist/[Random Pick with Blacklist]
4994-
//|{source_base_url}/_0710_RandomPickWithBlacklist.java[Java]
4995-
//|{doc_base_url}/0710-random-pick-with-blacklist.adoc[题解]
4996-
//|Hard
4997-
//|
4998-
//
4991+
4992+
|710
4993+
|{leetcode_base_url}/random-pick-with-blacklist/[Random Pick with Blacklist]
4994+
|{source_base_url}/_0710_RandomPickWithBlacklist.java[Java]
4995+
|{doc_base_url}/0710-random-pick-with-blacklist.adoc[题解]
4996+
|Hard
4997+
|
4998+
49994999
//|711
50005000
//|{leetcode_base_url}/number-of-distinct-islands-ii/[Number of Distinct Islands II]
50015001
//|{source_base_url}/_0711_NumberOfDistinctIslandsII.java[Java]

docs/0710-random-pick-with-blacklist.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ Optimize it such that it minimizes the call to system’s `Math.random()`.
5959

6060
The input is two lists: the subroutines called and their arguments. `Solution`'s constructor has two arguments, `N` and the blacklist `B`. `pick` has no arguments. Arguments are always wrapped with a list, even if there aren't any.
6161
62+
== 思路分析
6263
64+
image::images/0710-01.png[{image_attr}]
65+
66+
image::images/0710-02.png[{image_attr}]
6367
6468
[[src-0710]]
6569
[{java_src_attr}]

docs/images/0710-01.png

3.56 KB
Loading

docs/images/0710-02.png

3.96 KB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,10 @@
244244
|{doc_base_url}/0380-insert-delete-getrandom-o1.adoc[题解]
245245
|数据结构应用
246246

247+
|710
248+
|{leetcode_base_url}/random-pick-with-blacklist/[Random Pick with Blacklist]
249+
|{doc_base_url}/0710-random-pick-with-blacklist.adoc[题解]
250+
|把映射处理好
251+
247252

248253
|===
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import java.util.*;
4+
5+
public class _0710_RandomPickWithBlacklist {
6+
class Solution {
7+
Map<Integer, Integer> b2w;
8+
Random random;
9+
int size;
10+
11+
/**
12+
* 把黑名单都放在最后
13+
*/
14+
public Solution(int n, int[] blacklist) {
15+
size = n - blacklist.length;
16+
random = new Random();
17+
b2w = new HashMap<>();
18+
List<Integer> sb = new ArrayList<>();
19+
List<Integer> bw = new ArrayList<>();
20+
Set<Integer> bb = new HashSet<>();
21+
for (int i = 0; i < blacklist.length; i++) {
22+
int b = blacklist[i];
23+
// 按照界限,把黑名单数字按照大小分开
24+
if (b >= size) {
25+
bb.add(b);
26+
} else {
27+
sb.add(b);
28+
}
29+
}
30+
for (int i = n - 1; i >= size; i--) {
31+
// 从最后的数字里找出不是黑名单的数字
32+
if (!bb.contains(i)) {
33+
bw.add(i);
34+
}
35+
}
36+
// 建立起黑→白的对应关系
37+
while (!sb.isEmpty() && !bw.isEmpty()) {
38+
Integer b = sb.remove(sb.size() - 1);
39+
Integer w = bw.remove(bw.size() - 1);
40+
b2w.put(b, w);
41+
}
42+
}
43+
44+
public int pick() {
45+
int val = random.nextInt(size);
46+
return b2w.getOrDefault(val, val);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)