Skip to content

Commit 17a7350

Browse files
authored
feat: update lc problems (doocs#3165)
1 parent afc8673 commit 17a7350

File tree

29 files changed

+340
-69
lines changed

29 files changed

+340
-69
lines changed

solution/0100-0199/0127.Word Ladder/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tags:
1818

1919
<!-- description:start -->
2020

21-
<p>字典&nbsp;<code>wordList</code> 中从单词 <code>beginWord</code><em>&nbsp;</em><code>endWord</code> 的 <strong>转换序列 </strong>是一个按下述规格形成的序列<meta charset="UTF-8" />&nbsp;<code>beginWord -&gt; s<sub>1</sub>&nbsp;-&gt; s<sub>2</sub>&nbsp;-&gt; ... -&gt; s<sub>k</sub></code>:</p>
21+
<p>字典&nbsp;<code>wordList</code> 中从单词 <code>beginWord</code><em>&nbsp;</em>&nbsp;<code>endWord</code> 的 <strong>转换序列 </strong>是一个按下述规格形成的序列<meta charset="UTF-8" />&nbsp;<code>beginWord -&gt; s<sub>1</sub>&nbsp;-&gt; s<sub>2</sub>&nbsp;-&gt; ... -&gt; s<sub>k</sub></code>:</p>
2222

2323
<ul>
2424
<li>每一对相邻的单词只差一个字母。</li>

solution/0200-0299/0230.Kth Smallest Element in a BST/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ tags:
1919

2020
<!-- description:start -->
2121

22-
<p>给定一个二叉搜索树的根节点 <code>root</code> ,和一个整数 <code>k</code> ,请你设计一个算法查找其中第 <code>k</code><strong> </strong>个最小元素(从 1 开始计数)。</p>
22+
<p>给定一个二叉搜索树的根节点 <code>root</code> ,和一个整数 <code>k</code> ,请你设计一个算法查找其中第&nbsp;<code>k</code><strong>&nbsp;</strong>小的元素(从 1 开始计数)。</p>
2323

24-
<p> </p>
24+
<p>&nbsp;</p>
2525

2626
<p><strong>示例 1:</strong></p>
2727
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0200-0299/0230.Kth%20Smallest%20Element%20in%20a%20BST/images/kthtree1.jpg" style="width: 212px; height: 301px;" />
@@ -37,19 +37,19 @@ tags:
3737
<strong>输出:</strong>3
3838
</pre>
3939

40-
<p> </p>
40+
<p>&nbsp;</p>
4141

42-
<p> </p>
42+
<p>&nbsp;</p>
4343

4444
<p><strong>提示:</strong></p>
4545

4646
<ul>
4747
<li>树中的节点数为 <code>n</code> 。</li>
48-
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
49-
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
48+
<li><code>1 &lt;= k &lt;= n &lt;= 10<sup>4</sup></code></li>
49+
<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
5050
</ul>
5151

52-
<p> </p>
52+
<p>&nbsp;</p>
5353

5454
<p><strong>进阶:</strong>如果二叉搜索树经常被修改(插入/删除操作)并且你需要频繁地查找第 <code>k</code> 小的值,你将如何优化算法?</p>
5555

solution/1400-1499/1438.Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ Therefore, the size of the longest subarray is 2.
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: Ordered Set + Sliding Window
79+
80+
We can enumerate each position as the right endpoint of the subarray, and find the leftmost left endpoint corresponding to it, such that the difference between the maximum and minimum values in the interval does not exceed $limit$. During the process, we use an ordered set to maintain the maximum and minimum values within the window.
81+
82+
The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array `nums`.
7983

8084
<!-- tabs:start -->
8185

solution/2700-2799/2732.Find a Good Subset of the Matrix/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ tags:
2727

2828
<p>更正式的,如果选出来的行子集大小(即行的数量)为 k,那么每一列的和至多为&nbsp;<code>floor(k / 2)</code>&nbsp;。</p>
2929

30-
<p>请你返回一个整数数组,它包含好子集的行下标,请你将子集中的元素&nbsp;<b>升序</b>&nbsp;返回。</p>
30+
<p>请你返回一个整数数组,它包含好子集的行下标,请你将其&nbsp;<b>升序</b>&nbsp;返回。</p>
3131

3232
<p>如果有多个好子集,你可以返回任意一个。如果没有好子集,请你返回一个空数组。</p>
3333

@@ -89,7 +89,7 @@ tags:
8989
我们可以从小到大考虑答案选择的行数 $k$。
9090

9191
- 如果 $k = 1$,每一列的和最大为 $0$,那么必须满足有一行的所有元素都是 $0$,否则无法满足条件。
92-
- 如果 $k = 2$,每一列的和最大为 $1$,那么必须存在有两行,且这两行的元素按位或之后的结果是 $0$,否则无法满足条件。
92+
- 如果 $k = 2$,每一列的和最大为 $1$,那么必须存在有两行,且这两行的元素按位与之后的结果是 $0$,否则无法满足条件。
9393
- 如果 $k = 3$,每一列的和最大也是 $1$。如果 $k = 2$ 不满足条件,那么 $k = 3$ 也一定不满足条件,所以我们不需要考虑所有 $k \gt 2$ 且 $k$ 为奇数的情况。
9494
- 如果 $k = 4$,每一列的和最大为 $2$,此时一定是 $k = 2$ 不满足条件,也就是说,任意选取两行,都存在至少一个列的和为 $2$。我们在 $4$ 行中任意选取 $2$ 行,一共有 $C_4^2 = 6$ 种选法,那么存在至少 $6$ 个 $2$ 的列。由于列数 $n \le 5$,所以一定存在至少一列的和大于 $2$,所以 $k = 4$ 也不满足条件。
9595
- 对于 $k \gt 4$ 且 $k$ 为偶数的情况,我们可以得出同样的结论,即 $k$ 一定不满足条件。

solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ The length of the chosen subset is 1.
8787
We can consider the number of rows $k$ chosen for the answer from smallest to largest.
8888

8989
- If $k = 1$, the maximum sum of each column is $0$. Therefore, there must be a row where all elements are $0$, otherwise, the condition cannot be met.
90-
- If $k = 2$, the maximum sum of each column is $1$. There must exist two rows, and the bitwise OR result of these two rows' elements is $0$, otherwise, the condition cannot be met.
90+
- If $k = 2$, the maximum sum of each column is $1$. There must exist two rows, and the bitwise AND result of these two rows' elements is $0$, otherwise, the condition cannot be met.
9191
- If $k = 3$, the maximum sum of each column is also $1$. If the condition for $k = 2$ is not met, then the condition for $k = 3$ will definitely not be met either. Therefore, we do not need to consider any case where $k > 2$ and $k$ is odd.
9292
- If $k = 4$, the maximum sum of each column is $2$. This situation definitely occurs when the condition for $k = 2$ is not met, meaning that for any two selected rows, there exists at least one column with a sum of $2$. When choosing any 2 rows out of 4, there are a total of $C_4^2 = 6$ ways to choose, so there are at least $6$ columns with a sum of $2$. Since the number of columns $n \le 5$, there must be at least one column with a sum greater than $2$, so the condition for $k = 4$ is also not met.
9393
- For $k > 4$ and $k$ being even, we can draw the same conclusion, that $k$ definitely does not meet the condition.

solution/2700-2799/2741.Special Permutations/README.md

+68-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ tags:
6262

6363
### 方法一:状态压缩动态规划
6464

65-
我们注意到题目中数组的长度最大不超过 $14$,因此,我们可以用一个整数来表示当前的状态,其中第 $i$ 位为 $1$ 表示数组中的第 $i$ 个数已经被选取,为 $0$ 表示数组中的第 $i$ 个数还未被选取。
65+
我们注意到题目中数组的长度最大不超过 $14$,因此,我们可以用一个二进制整数来表示当前的状态,其中第 $i$ 位为 $1$ 表示数组中的第 $i$ 个数已经被选取,为 $0$ 表示数组中的第 $i$ 个数还未被选取。
6666

6767
我们定义 $f[i][j]$ 表示当前选取的整数状态为 $i$,且最后一个选取的整数下标为 $j$ 的方案数。初始时 $f[0][0]=0$,答案为 $\sum_{j=0}^{n-1}f[2^n-1][j]$。
6868

@@ -208,6 +208,73 @@ func specialPerm(nums []int) (ans int) {
208208
}
209209
```
210210

211+
#### TypeScript
212+
213+
```ts
214+
function specialPerm(nums: number[]): number {
215+
const mod = 1e9 + 7;
216+
const n = nums.length;
217+
const m = 1 << n;
218+
const f = Array.from({ length: m }, () => Array(n).fill(0));
219+
220+
for (let i = 1; i < m; ++i) {
221+
for (let j = 0; j < n; ++j) {
222+
if (((i >> j) & 1) === 1) {
223+
const ii = i ^ (1 << j);
224+
if (ii === 0) {
225+
f[i][j] = 1;
226+
continue;
227+
}
228+
for (let k = 0; k < n; ++k) {
229+
if (nums[j] % nums[k] === 0 || nums[k] % nums[j] === 0) {
230+
f[i][j] = (f[i][j] + f[ii][k]) % mod;
231+
}
232+
}
233+
}
234+
}
235+
}
236+
237+
return f[m - 1].reduce((acc, x) => (acc + x) % mod);
238+
}
239+
```
240+
241+
#### Rust
242+
243+
```rust
244+
impl Solution {
245+
pub fn special_perm(nums: Vec<i32>) -> i32 {
246+
const MOD: i32 = 1_000_000_007;
247+
let n = nums.len();
248+
let m = 1 << n;
249+
let mut f = vec![vec![0; n]; m];
250+
251+
for i in 1..m {
252+
for j in 0..n {
253+
if (i >> j) & 1 == 1 {
254+
let ii = i ^ (1 << j);
255+
if ii == 0 {
256+
f[i][j] = 1;
257+
continue;
258+
}
259+
for k in 0..n {
260+
if nums[j] % nums[k] == 0 || nums[k] % nums[j] == 0 {
261+
f[i][j] = (f[i][j] + f[ii][k]) % MOD;
262+
}
263+
}
264+
}
265+
}
266+
}
267+
268+
let mut ans = 0;
269+
for &x in &f[m - 1] {
270+
ans = (ans + x) % MOD;
271+
}
272+
273+
ans
274+
}
275+
}
276+
```
277+
211278
<!-- tabs:end -->
212279

213280
<!-- solution:end -->

solution/2700-2799/2741.Special Permutations/README_EN.md

+67
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,73 @@ func specialPerm(nums []int) (ans int) {
208208
}
209209
```
210210

211+
#### TypeScript
212+
213+
```ts
214+
function specialPerm(nums: number[]): number {
215+
const mod = 1e9 + 7;
216+
const n = nums.length;
217+
const m = 1 << n;
218+
const f = Array.from({ length: m }, () => Array(n).fill(0));
219+
220+
for (let i = 1; i < m; ++i) {
221+
for (let j = 0; j < n; ++j) {
222+
if (((i >> j) & 1) === 1) {
223+
const ii = i ^ (1 << j);
224+
if (ii === 0) {
225+
f[i][j] = 1;
226+
continue;
227+
}
228+
for (let k = 0; k < n; ++k) {
229+
if (nums[j] % nums[k] === 0 || nums[k] % nums[j] === 0) {
230+
f[i][j] = (f[i][j] + f[ii][k]) % mod;
231+
}
232+
}
233+
}
234+
}
235+
}
236+
237+
return f[m - 1].reduce((acc, x) => (acc + x) % mod);
238+
}
239+
```
240+
241+
#### Rust
242+
243+
```rust
244+
impl Solution {
245+
pub fn special_perm(nums: Vec<i32>) -> i32 {
246+
const MOD: i32 = 1_000_000_007;
247+
let n = nums.len();
248+
let m = 1 << n;
249+
let mut f = vec![vec![0; n]; m];
250+
251+
for i in 1..m {
252+
for j in 0..n {
253+
if (i >> j) & 1 == 1 {
254+
let ii = i ^ (1 << j);
255+
if ii == 0 {
256+
f[i][j] = 1;
257+
continue;
258+
}
259+
for k in 0..n {
260+
if nums[j] % nums[k] == 0 || nums[k] % nums[j] == 0 {
261+
f[i][j] = (f[i][j] + f[ii][k]) % MOD;
262+
}
263+
}
264+
}
265+
}
266+
}
267+
268+
let mut ans = 0;
269+
for &x in &f[m - 1] {
270+
ans = (ans + x) % MOD;
271+
}
272+
273+
ans
274+
}
275+
}
276+
```
277+
211278
<!-- tabs:end -->
212279

213280
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
impl Solution {
2+
pub fn special_perm(nums: Vec<i32>) -> i32 {
3+
const MOD: i32 = 1_000_000_007;
4+
let n = nums.len();
5+
let m = 1 << n;
6+
let mut f = vec![vec![0; n]; m];
7+
8+
for i in 1..m {
9+
for j in 0..n {
10+
if (i >> j) & 1 == 1 {
11+
let ii = i ^ (1 << j);
12+
if ii == 0 {
13+
f[i][j] = 1;
14+
continue;
15+
}
16+
for k in 0..n {
17+
if nums[j] % nums[k] == 0 || nums[k] % nums[j] == 0 {
18+
f[i][j] = (f[i][j] + f[ii][k]) % MOD;
19+
}
20+
}
21+
}
22+
}
23+
}
24+
25+
let mut ans = 0;
26+
for &x in &f[m - 1] {
27+
ans = (ans + x) % MOD;
28+
}
29+
30+
ans
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function specialPerm(nums: number[]): number {
2+
const mod = 1e9 + 7;
3+
const n = nums.length;
4+
const m = 1 << n;
5+
const f = Array.from({ length: m }, () => Array(n).fill(0));
6+
7+
for (let i = 1; i < m; ++i) {
8+
for (let j = 0; j < n; ++j) {
9+
if (((i >> j) & 1) === 1) {
10+
const ii = i ^ (1 << j);
11+
if (ii === 0) {
12+
f[i][j] = 1;
13+
continue;
14+
}
15+
for (let k = 0; k < n; ++k) {
16+
if (nums[j] % nums[k] === 0 || nums[k] % nums[j] === 0) {
17+
f[i][j] = (f[i][j] + f[ii][k]) % mod;
18+
}
19+
}
20+
}
21+
}
22+
}
23+
24+
return f[m - 1].reduce((acc, x) => (acc + x) % mod);
25+
}

solution/2900-2999/2966.Divide Array Into Arrays With Max Difference/README_EN.md

+42-18
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,67 @@ tags:
2020

2121
<!-- description:start -->
2222

23-
<p>You are given an integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>
23+
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is a multiple of 3 and a positive integer <code>k</code>.</p>
2424

25-
<p>Divide the array into one or more arrays of size <code>3</code> satisfying the following conditions:</p>
25+
<p>Divide the array <code>nums</code> into <code>n / 3</code> arrays of size <strong>3</strong> satisfying the following condition:</p>
2626

2727
<ul>
28-
<li><strong>Each</strong> element of <code>nums</code> should be in <strong>exactly</strong> one array.</li>
29-
<li>The difference between <strong>any</strong> two elements in one array is less than or equal to <code>k</code>.</li>
28+
<li>The difference between <strong>any</strong> two elements in one array is <strong>less than or equal</strong> to <code>k</code>.</li>
3029
</ul>
3130

32-
<p>Return <em>a </em><strong>2D</strong><em> array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return <strong>any</strong> of them.</em></p>
31+
<p>Return a <strong>2D</strong> array containing the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return <strong>any</strong> of them.</p>
3332

3433
<p>&nbsp;</p>
3534
<p><strong class="example">Example 1:</strong></p>
3635

37-
<pre>
38-
<strong>Input:</strong> nums = [1,3,4,8,7,9,3,5,1], k = 2
39-
<strong>Output:</strong> [[1,1,3],[3,4,5],[7,8,9]]
40-
<strong>Explanation:</strong> We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
41-
The difference between any two elements in each array is less than or equal to 2.
42-
Note that the order of elements is not important.
43-
</pre>
36+
<div class="example-block">
37+
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,4,8,7,9,3,5,1], k = 2</span></p>
38+
39+
<p><strong>Output:</strong> <span class="example-io">[[1,1,3],[3,4,5],[7,8,9]]</span></p>
40+
41+
<p><strong>Explanation:</strong></p>
42+
43+
<p>The difference between any two elements in each array is less than or equal to 2.</p>
44+
</div>
4445

4546
<p><strong class="example">Example 2:</strong></p>
4647

47-
<pre>
48-
<strong>Input:</strong> nums = [1,3,3,2,7,3], k = 3
49-
<strong>Output:</strong> []
50-
<strong>Explanation:</strong> It is not possible to divide the array satisfying all the conditions.
51-
</pre>
48+
<div class="example-block">
49+
<p><strong>Input:</strong> <span class="example-io">nums = [2,4,2,2,5,2], k = 2</span></p>
50+
51+
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
52+
53+
<p><strong>Explanation:</strong></p>
54+
55+
<p>Different ways to divide <code>nums</code> into 2 arrays of size 3 are:</p>
56+
57+
<ul>
58+
<li>[[2,2,2],[2,4,5]] (and its permutations)</li>
59+
<li>[[2,2,4],[2,2,5]] (and its permutations)</li>
60+
</ul>
61+
62+
<p>Because there are four 2s there will be an array with the elements 2 and 5 no matter how we divide it. since <code>5 - 2 = 3 &gt; k</code>, the condition is not satisfied and so there is no valid division.</p>
63+
</div>
64+
65+
<p><strong class="example">Example 3:</strong></p>
66+
67+
<div class="example-block">
68+
<p><strong>Input:</strong> <span class="example-io">nums = [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], k = 14</span></p>
69+
70+
<p><strong>Output:</strong> <span class="example-io">[[2,2,12],[4,8,5],[5,9,7],[7,8,5],[5,9,10],[11,12,2]]</span></p>
71+
72+
<p><strong>Explanation:</strong></p>
73+
74+
<p>The difference between any two elements in each array is less than or equal to 14.</p>
75+
</div>
5276

5377
<p>&nbsp;</p>
5478
<p><strong>Constraints:</strong></p>
5579

5680
<ul>
5781
<li><code>n == nums.length</code></li>
5882
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
59-
<li><code>n</code> is a multiple of <code>3</code>.</li>
83+
<li><code>n </code>is a multiple of 3</li>
6084
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
6185
<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
6286
</ul>

solution/3100-3199/3188.Find Top Scoring Students II/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
comments: true
33
difficulty: 困难
44
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md
5+
tags:
6+
- 数据库
57
---
68

79
<!-- problem:start -->

0 commit comments

Comments
 (0)