Skip to content

feat: update lc problems #3165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/0100-0199/0127.Word Ladder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tags:

<!-- description:start -->

<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>
<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>

<ul>
<li>每一对相邻的单词只差一个字母。</li>
Expand Down
14 changes: 7 additions & 7 deletions solution/0200-0299/0230.Kth Smallest Element in a BST/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ tags:

<!-- description:start -->

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

<p> </p>
<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>
<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;" />
Expand All @@ -37,19 +37,19 @@ tags:
<strong>输出:</strong>3
</pre>

<p> </p>
<p>&nbsp;</p>

<p> </p>
<p>&nbsp;</p>

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

<ul>
<li>树中的节点数为 <code>n</code> 。</li>
<li><code>1 <= k <= n <= 10<sup>4</sup></code></li>
<li><code>0 <= Node.val <= 10<sup>4</sup></code></li>
<li><code>1 &lt;= k &lt;= n &lt;= 10<sup>4</sup></code></li>
<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul>

<p> </p>
<p>&nbsp;</p>

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ Therefore, the size of the longest subarray is 2.

<!-- solution:start -->

### Solution 1
### Solution 1: Ordered Set + Sliding Window

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.

The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array `nums`.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tags:

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

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

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

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

- 如果 $k = 1$,每一列的和最大为 $0$,那么必须满足有一行的所有元素都是 $0$,否则无法满足条件。
- 如果 $k = 2$,每一列的和最大为 $1$,那么必须存在有两行,且这两行的元素按位或之后的结果是 $0$,否则无法满足条件。
- 如果 $k = 2$,每一列的和最大为 $1$,那么必须存在有两行,且这两行的元素按位与之后的结果是 $0$,否则无法满足条件。
- 如果 $k = 3$,每一列的和最大也是 $1$。如果 $k = 2$ 不满足条件,那么 $k = 3$ 也一定不满足条件,所以我们不需要考虑所有 $k \gt 2$ 且 $k$ 为奇数的情况。
- 如果 $k = 4$,每一列的和最大为 $2$,此时一定是 $k = 2$ 不满足条件,也就是说,任意选取两行,都存在至少一个列的和为 $2$。我们在 $4$ 行中任意选取 $2$ 行,一共有 $C_4^2 = 6$ 种选法,那么存在至少 $6$ 个 $2$ 的列。由于列数 $n \le 5$,所以一定存在至少一列的和大于 $2$,所以 $k = 4$ 也不满足条件。
- 对于 $k \gt 4$ 且 $k$ 为偶数的情况,我们可以得出同样的结论,即 $k$ 一定不满足条件。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ The length of the chosen subset is 1.
We can consider the number of rows $k$ chosen for the answer from smallest to largest.

- 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.
- 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.
- 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.
- 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.
- 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.
- For $k > 4$ and $k$ being even, we can draw the same conclusion, that $k$ definitely does not meet the condition.
Expand Down
69 changes: 68 additions & 1 deletion solution/2700-2799/2741.Special Permutations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ tags:

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

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

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

Expand Down Expand Up @@ -208,6 +208,73 @@ func specialPerm(nums []int) (ans int) {
}
```

#### TypeScript

```ts
function specialPerm(nums: number[]): number {
const mod = 1e9 + 7;
const n = nums.length;
const m = 1 << n;
const f = Array.from({ length: m }, () => Array(n).fill(0));

for (let i = 1; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (((i >> j) & 1) === 1) {
const ii = i ^ (1 << j);
if (ii === 0) {
f[i][j] = 1;
continue;
}
for (let k = 0; k < n; ++k) {
if (nums[j] % nums[k] === 0 || nums[k] % nums[j] === 0) {
f[i][j] = (f[i][j] + f[ii][k]) % mod;
}
}
}
}
}

return f[m - 1].reduce((acc, x) => (acc + x) % mod);
}
```

#### Rust

```rust
impl Solution {
pub fn special_perm(nums: Vec<i32>) -> i32 {
const MOD: i32 = 1_000_000_007;
let n = nums.len();
let m = 1 << n;
let mut f = vec![vec![0; n]; m];

for i in 1..m {
for j in 0..n {
if (i >> j) & 1 == 1 {
let ii = i ^ (1 << j);
if ii == 0 {
f[i][j] = 1;
continue;
}
for k in 0..n {
if nums[j] % nums[k] == 0 || nums[k] % nums[j] == 0 {
f[i][j] = (f[i][j] + f[ii][k]) % MOD;
}
}
}
}
}

let mut ans = 0;
for &x in &f[m - 1] {
ans = (ans + x) % MOD;
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
67 changes: 67 additions & 0 deletions solution/2700-2799/2741.Special Permutations/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,73 @@ func specialPerm(nums []int) (ans int) {
}
```

#### TypeScript

```ts
function specialPerm(nums: number[]): number {
const mod = 1e9 + 7;
const n = nums.length;
const m = 1 << n;
const f = Array.from({ length: m }, () => Array(n).fill(0));

for (let i = 1; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (((i >> j) & 1) === 1) {
const ii = i ^ (1 << j);
if (ii === 0) {
f[i][j] = 1;
continue;
}
for (let k = 0; k < n; ++k) {
if (nums[j] % nums[k] === 0 || nums[k] % nums[j] === 0) {
f[i][j] = (f[i][j] + f[ii][k]) % mod;
}
}
}
}
}

return f[m - 1].reduce((acc, x) => (acc + x) % mod);
}
```

#### Rust

```rust
impl Solution {
pub fn special_perm(nums: Vec<i32>) -> i32 {
const MOD: i32 = 1_000_000_007;
let n = nums.len();
let m = 1 << n;
let mut f = vec![vec![0; n]; m];

for i in 1..m {
for j in 0..n {
if (i >> j) & 1 == 1 {
let ii = i ^ (1 << j);
if ii == 0 {
f[i][j] = 1;
continue;
}
for k in 0..n {
if nums[j] % nums[k] == 0 || nums[k] % nums[j] == 0 {
f[i][j] = (f[i][j] + f[ii][k]) % MOD;
}
}
}
}
}

let mut ans = 0;
for &x in &f[m - 1] {
ans = (ans + x) % MOD;
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
32 changes: 32 additions & 0 deletions solution/2700-2799/2741.Special Permutations/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
impl Solution {
pub fn special_perm(nums: Vec<i32>) -> i32 {
const MOD: i32 = 1_000_000_007;
let n = nums.len();
let m = 1 << n;
let mut f = vec![vec![0; n]; m];

for i in 1..m {
for j in 0..n {
if (i >> j) & 1 == 1 {
let ii = i ^ (1 << j);
if ii == 0 {
f[i][j] = 1;
continue;
}
for k in 0..n {
if nums[j] % nums[k] == 0 || nums[k] % nums[j] == 0 {
f[i][j] = (f[i][j] + f[ii][k]) % MOD;
}
}
}
}
}

let mut ans = 0;
for &x in &f[m - 1] {
ans = (ans + x) % MOD;
}

ans
}
}
25 changes: 25 additions & 0 deletions solution/2700-2799/2741.Special Permutations/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function specialPerm(nums: number[]): number {
const mod = 1e9 + 7;
const n = nums.length;
const m = 1 << n;
const f = Array.from({ length: m }, () => Array(n).fill(0));

for (let i = 1; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (((i >> j) & 1) === 1) {
const ii = i ^ (1 << j);
if (ii === 0) {
f[i][j] = 1;
continue;
}
for (let k = 0; k < n; ++k) {
if (nums[j] % nums[k] === 0 || nums[k] % nums[j] === 0) {
f[i][j] = (f[i][j] + f[ii][k]) % mod;
}
}
}
}
}

return f[m - 1].reduce((acc, x) => (acc + x) % mod);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,67 @@ tags:

<!-- description:start -->

<p>You are given an integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>
<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>

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

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

<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>
<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>

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

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

<p><strong>Output:</strong> <span class="example-io">[[1,1,3],[3,4,5],[7,8,9]]</span></p>

<p><strong>Explanation:</strong></p>

<p>The difference between any two elements in each array is less than or equal to 2.</p>
</div>

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

<pre>
<strong>Input:</strong> nums = [1,3,3,2,7,3], k = 3
<strong>Output:</strong> []
<strong>Explanation:</strong> It is not possible to divide the array satisfying all the conditions.
</pre>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,4,2,2,5,2], k = 2</span></p>

<p><strong>Output:</strong> <span class="example-io">[]</span></p>

<p><strong>Explanation:</strong></p>

<p>Different ways to divide <code>nums</code> into 2 arrays of size 3 are:</p>

<ul>
<li>[[2,2,2],[2,4,5]] (and its permutations)</li>
<li>[[2,2,4],[2,2,5]] (and its permutations)</li>
</ul>

<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>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<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>

<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>

<p><strong>Explanation:</strong></p>

<p>The difference between any two elements in each array is less than or equal to 14.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>n == nums.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>n</code> is a multiple of <code>3</code>.</li>
<li><code>n </code>is a multiple of 3</li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3188.Find%20Top%20Scoring%20Students%20II/README.md
tags:
- 数据库
---

<!-- problem:start -->
Expand Down
Loading
Loading