Skip to content

Commit d5add46

Browse files
committed
✨feat: Add 398
1 parent 9028c46 commit d5add46

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

Index/蓄水池抽样.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
| 题目 | 题解 | 难度 | 推荐指数 |
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
33
| [382. 链表随机节点](https://leetcode-cn.com/problems/linked-list-random-node/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/linked-list-random-node/solution/gong-shui-san-xie-xu-shui-chi-chou-yang-1lp9d/) | 中等 | 🤩🤩🤩🤩🤩 |
4+
| [398. 随机数索引](https://leetcode-cn.com/problems/random-pick-index/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-pick-index/solution/by-ac_oier-zhml/) | 中等 | 🤩🤩🤩🤩🤩 |
45

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[398. 随机数索引](https://leetcode-cn.com/problems/random-pick-index/solution/by-ac_oier-zhml/)** ,难度为 **中等**
4+
5+
Tag : 「哈希表」、「模拟」、「随机化」、「蓄水池抽样」
6+
7+
8+
9+
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引。 您可以假设给定的数字一定存在于数组中。
10+
11+
注意:
12+
13+
数组大小可能非常大。 使用太多额外空间的解决方案将不会通过测试。
14+
15+
示例:
16+
```
17+
int[] nums = new int[] {1,2,3,3,3};
18+
Solution solution = new Solution(nums);
19+
20+
// pick(3) 应该返回索引 2,3 或者 4。每个索引的返回概率应该相等。
21+
solution.pick(3);
22+
23+
// pick(1) 应该返回 0。因为只有nums[0]等于1。
24+
solution.pick(1);
25+
```
26+
27+
提示:
28+
* $1 <= nums.length <= 2 * 10^4$
29+
* $-2^{31} <= nums[i] <= 2^{31} - 1$
30+
* `target` 确保存在于 `nums`
31+
* 最多调用 $10^4$ 次的 `pick`
32+
33+
---
34+
35+
### 哈希表 预处理(定长数据流)
36+
37+
切换英文补全一下数据范围:
38+
* $1 <= nums.length <= 2 * 10^4$
39+
* $-2^{31} <= nums[i] <= 2^{31} - 1$
40+
* `target` 确保存在于 `nums`
41+
* 最多调用 $10^4$ 次的 `pick`
42+
43+
为了方便,我们令 `nums` 的长度为 $n$,利用 $n$ 的数据范围为 $2 * 10^4$,且完整的数组为初始化时已给出,我们可以通过使用「哈希表 + 预处理」的方式进行求解。
44+
45+
具体的,在构造函数传入 `nums` 时,遍历 `nums` 并存储每个 $nums[i]$ 对应的下标集合,即使用哈希表以 $nums[i]$ 为键,下标集合 `List` 作为值进行存储。
46+
47+
`pick` 操作时,通过 $O(1)$ 的复杂度取出所有 $nums[i] = target$ 的集合下标,再随机一个下标进行返回。
48+
49+
代码:
50+
```Java
51+
class Solution {
52+
Random random = new Random();
53+
Map<Integer, List<Integer>> map = new HashMap<>();
54+
public Solution(int[] nums) {
55+
int n = nums.length;
56+
for (int i = 0; i < n; i++) {
57+
List<Integer> list = map.getOrDefault(nums[i], new ArrayList<>());
58+
list.add(i);
59+
map.put(nums[i], list);
60+
}
61+
}
62+
public int pick(int target) {
63+
List<Integer> list = map.get(target);
64+
return list.get(random.nextInt(list.size()));
65+
}
66+
}
67+
```
68+
* 时间复杂度:初始化的复杂度为 $O(n)$;`pick` 操作的复杂度为 $O(1)$
69+
* 空间复杂度:$O(n)$
70+
71+
---
72+
73+
### 蓄水池抽样(不定长数据流)
74+
75+
`nums` 并不是在初始化时完全给出,而是持续以「流」的形式给出,且数据流的很长,不便进行预处理的话,我们只能使用「蓄水池抽样」的方式求解。
76+
77+
具体的,我们在每次 `pick` 时对流进行遍历,由于数据流很大,我们不能在遍历过程中使用诸如数组的容器存储所有满足条件的下标,只能对于每个 $nums[i] = target$ 执行「是否要将 $i$ 作为最新答案候选」的操作。
78+
79+
假设共有 $m$ 个下标满足 $nums[i] = target$,我们需要做到以 $\frac{1}{m}$ 概率返回任一坐标。
80+
81+
我们规定当遇到第 $k$ 个满足 $nums[i] = target$ 的下标时,执行一次 $[0, k)$ 的随机操作,当随机结果为 $0$ 时(发生概率为 $\frac{1}{k}$),我们将该坐标作为最新的答案候选。
82+
83+
当对每一个 $nums[i] = target$ 的下标都进行上述操作后,容易证明每一位下标返回的概率均为 $\frac{1}{m}$。
84+
85+
假设最后返回的是第 $k$ 个满足条件的下标,发生概率为 = 第 $k$ 个下标被候选的概率 $\times$ 后面 $k + 1$ 到 $m$ 个下标不被候选的概率 = $\frac{1}{k} \times (1 - \frac{1}{k + 1}) \times ... \times (1 - \frac{1}{m})$ = $\frac{1}{m}$ 。
86+
87+
代码:
88+
```Java
89+
class Solution {
90+
Random random = new Random();
91+
int[] nums;
92+
public Solution(int[] _nums) {
93+
nums = _nums;
94+
}
95+
public int pick(int target) {
96+
int n = nums.length, ans = 0;
97+
for (int i = 0, cnt = 0; i < n; i++) {
98+
if (nums[i] == target) {
99+
cnt++;
100+
if (random.nextInt(cnt) == 0) ans = i;
101+
}
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
* 时间复杂度:初始化的复杂度为 $O(1)$;`pick` 操作的复杂度为 $O(n)$
108+
* 空间复杂度:$O(n)$
109+
110+
---
111+
112+
### 最后
113+
114+
这是我们「刷穿 LeetCode」系列文章的第 `No.398` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
115+
116+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
117+
118+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
119+
120+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
121+

0 commit comments

Comments
 (0)