Skip to content

Commit ff21aa7

Browse files
committed
feat: leetcode 710
1 parent 4ef3376 commit ff21aa7

File tree

8 files changed

+101
-1
lines changed

8 files changed

+101
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:413-green" alt="进度:413">
5+
<img src="https://img.shields.io/badge/-进度:414-green" alt="进度:414">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

assets/data/categories.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@
206206
"path": "./problemset/two-sum-iv-input-is-a-bst/README.md",
207207
"difficulty": "简单"
208208
},
209+
{
210+
"id": "710",
211+
"title": "黑名单中的随机数",
212+
"path": "./problemset/random-pick-with-blacklist/README.md",
213+
"difficulty": "困难"
214+
},
209215
{
210216
"id": "720",
211217
"title": "词典中最长的单词",

assets/data/topics.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3239,6 +3239,16 @@
32393239
"url": "https://leetcode-cn.com/problems/to-lower-case/",
32403240
"path": "./problemset/to-lower-case/README.md"
32413241
},
3242+
{
3243+
"id": "710",
3244+
"title": {
3245+
"cn": "黑名单中的随机数",
3246+
"en": "random-pick-with-blacklist"
3247+
},
3248+
"difficulty": "困难",
3249+
"url": "https://leetcode.cn/problems/random-pick-with-blacklist/",
3250+
"path": "./problemset/random-pick-with-blacklist/README.md"
3251+
},
32423252
{
32433253
"id": "713",
32443254
"title": {

assets/docs/CATEGORIES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
| 532. [数组中的 k-diff 数对](../../problemset/k-diff-pairs-in-an-array/README.md) | 中等 |
3939
| 599. [两个列表的最小索引总和](../../problemset/minimum-index-sum-of-two-lists/README.md) | 简单 |
4040
| 653. [两数之和 IV - 输入 BST](../../problemset/two-sum-iv-input-is-a-bst/README.md) | 简单 |
41+
| 710. [黑名单中的随机数](../../problemset/random-pick-with-blacklist/README.md) | 困难 |
4142
| 720. [词典中最长的单词](../../problemset/longest-word-in-dictionary/README.md) | 简单 |
4243
| 804. [唯一摩尔斯密码词](../../problemset/unique-morse-code-words/README.md) | 简单 |
4344
| 819. [最常见的单词](../../problemset/most-common-word/README.md) | 简单 |

assets/docs/TOPICS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@
648648

649649
[709. 转换成小写字母](../../problemset/to-lower-case/README.md)
650650

651+
[710. 黑名单中的随机数](../../problemset/random-pick-with-blacklist/README.md)
652+
651653
[713. 乘积小于 K 的子数组](../../problemset/subarray-product-less-than-k/README.md)
652654

653655
[715. Range模块](../../problemset/range-module/README.md)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 黑名单中的随机数
2+
3+
> 难度:困难
4+
>
5+
> https://leetcode.cn/problems/random-pick-with-blacklist/
6+
7+
## 题目
8+
9+
给定一个整数 `n` 和一个 `无重复` 黑名单整数数组 `blacklist` 。设计一种算法,从 `[0, n - 1]` 范围内的任意整数中选取一个 **未加入** 黑名单 `blacklist` 的整数。任何在上述范围内且不在黑名单 `blacklist` 中的整数都应该有 **同等的可能性** 被返回。
10+
11+
优化你的算法,使它最小化调用语言 **内置** 随机函数的次数。
12+
13+
实现 `Solution` 类:
14+
15+
- Solution(int n, int[] blacklist) 初始化整数 n 和被加入黑名单 blacklist 的整数
16+
- int pick() 返回一个范围为 [0, n - 1] 且不在黑名单 blacklist 中的随机整数
17+
 
18+
19+
### 示例:
20+
21+
```
22+
输入
23+
["Solution", "pick", "pick", "pick", "pick", "pick", "pick", "pick"]
24+
[[7, [2, 3, 5]], [], [], [], [], [], [], []]
25+
输出
26+
[null, 0, 4, 1, 6, 1, 0, 4]
27+
28+
解释
29+
Solution solution = new Solution(7, [2, 3, 5]);
30+
solution.pick(); // 返回0,任何[0,1,4,6]的整数都可以。注意,对于每一个pick的调用,
31+
// 0、1、4和6的返回概率必须相等(即概率为1/4)。
32+
solution.pick(); // 返回 4
33+
solution.pick(); // 返回 1
34+
solution.pick(); // 返回 6
35+
solution.pick(); // 返回 1
36+
solution.pick(); // 返回 0
37+
solution.pick(); // 返回 4
38+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect, it } from 'vitest'
2+
import { Solution } from '.'
3+
4+
it('黑名单中的随机数', () => {
5+
const solution = new Solution(7, [2, 3, 5])
6+
expect([0, 1, 4, 6]).toContain(solution.pick())
7+
expect([0, 1, 4, 6]).toContain(solution.pick())
8+
expect([0, 1, 4, 6]).toContain(solution.pick())
9+
expect([0, 1, 4, 6]).toContain(solution.pick())
10+
expect([0, 1, 4, 6]).toContain(solution.pick())
11+
expect([0, 1, 4, 6]).toContain(solution.pick())
12+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export class Solution {
2+
bound: number
3+
b2w: Map<number, number>
4+
5+
constructor(n: number, blacklist: number[]) {
6+
this.b2w = new Map<number, number>()
7+
const len = blacklist.length
8+
this.bound = n - len
9+
const black = new Set()
10+
11+
for (const b of blacklist) {
12+
if (b >= this.bound)
13+
black.add(b)
14+
}
15+
16+
let w = this.bound
17+
for (const b of blacklist) {
18+
if (b < this.bound) {
19+
while (black.has(w)) w++
20+
21+
this.b2w.set(b, w)
22+
w++
23+
}
24+
}
25+
}
26+
27+
pick(): number {
28+
const x = Math.floor(Math.random() * this.bound)
29+
return this.b2w.get(x) || x
30+
}
31+
}

0 commit comments

Comments
 (0)