Skip to content

Commit 0980c36

Browse files
authored
feat: update lc problems (#2301)
1 parent 204cd88 commit 0980c36

File tree

38 files changed

+460
-78
lines changed

38 files changed

+460
-78
lines changed

solution/0300-0399/0321.Create Maximum Number/README.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
<p><strong>示例&nbsp;1:</strong></p>
1616

17-
<pre><strong>输入:</strong>
17+
<pre>
18+
<strong>输入:</strong>
1819
nums1 = <code>[3, 4, 6, 5]</code>
1920
nums2 = <code>[9, 1, 2, 5, 8, 3]</code>
2021
k = <code>5</code>
@@ -23,7 +24,8 @@ k = <code>5</code>
2324

2425
<p><strong>示例 2:</strong></p>
2526

26-
<pre><strong>输入:</strong>
27+
<pre>
28+
<strong>输入:</strong>
2729
nums1 = <code>[6, 7]</code>
2830
nums2 = <code>[6, 0, 4]</code>
2931
k = <code>5</code>
@@ -32,12 +34,24 @@ k = <code>5</code>
3234

3335
<p><strong>示例 3:</strong></p>
3436

35-
<pre><strong>输入:</strong>
37+
<pre>
38+
<strong>输入:</strong>
3639
nums1 = <code>[3, 9]</code>
3740
nums2 = <code>[8, 9]</code>
3841
k = <code>3</code>
3942
<strong>输出:</strong>
40-
<code>[9, 8, 9]</code></pre>
43+
<code>[9, 8, 9]</code>
44+
</pre>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>m == nums1.length</code></li>
50+
<li><code>n == nums2.length</code></li>
51+
<li><code>1 &lt;= m, n &lt;= 500</code></li>
52+
<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 9</code></li>
53+
<li><code>1 &lt;= k &lt;= m + n</code></li>
54+
</ul>
4155

4256
## 解法
4357

solution/0800-0899/0899.Orderly Queue/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<p>给定一个字符串 <code>s</code> 和一个整数 <code>k</code>&nbsp;。你可以从 <code>s</code> 的前 <code>k</code> 个字母中选择一个,并把它加到字符串的末尾。</p>
1010

11-
<p>返回 <em>在应用上述步骤的任意数量的移动后,字典上最小的字符串&nbsp;</em>。</p>
11+
<p>返回 <em>在应用上述步骤的任意数量的移动后,字典序最小的字符串&nbsp;</em>。</p>
1212

1313
<p>&nbsp;</p>
1414

solution/1300-1399/1386.Cinema Seat Allocation/README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,37 @@
66

77
<!-- 这里写题目描述 -->
88

9-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1386.Cinema%20Seat%20Allocation/images/cinema_seats_1.png" style="height: 149px; width: 400px;"></p>
9+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1386.Cinema%20Seat%20Allocation/images/cinema_seats_1.png" style="height: 149px; width: 400px;" /></p>
1010

1111
<p>如上图所示,电影院的观影厅中有 <code>n</code>&nbsp;行座位,行编号从 1&nbsp;到 <code>n</code>&nbsp;,且每一行内总共有 10 个座位,列编号从 1 到 10 。</p>
1212

13-
<p>给你数组&nbsp;<code>reservedSeats</code>&nbsp;,包含所有已经被预约了的座位。比如说,<code>researvedSeats[i]=[3,8]</code>&nbsp;,它表示第&nbsp;<strong>3</strong>&nbsp;行第&nbsp;<strong>8</strong>&nbsp;个座位被预约了。</p>
13+
<p>给你数组&nbsp;<code>reservedSeats</code>&nbsp;,包含所有已经被预约了的座位。比如说,<code>reservedSeats[i]=[3,8]</code>&nbsp;,它表示第&nbsp;<strong>3</strong>&nbsp;行第&nbsp;<strong>8</strong>&nbsp;个座位被预约了。</p>
1414

1515
<p>请你返回&nbsp;<strong>最多能安排多少个 4 人家庭</strong>&nbsp;。4 人家庭要占据&nbsp;<strong>同一行内连续&nbsp;</strong>的 4 个座位。隔着过道的座位(比方说 [3,3] 和 [3,4])不是连续的座位,但是如果你可以将 4 人家庭拆成过道两边各坐 2 人,这样子是允许的。</p>
1616

1717
<p>&nbsp;</p>
1818

1919
<p><strong>示例 1:</strong></p>
2020

21-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1386.Cinema%20Seat%20Allocation/images/cinema_seats_3.png" style="height: 96px; width: 400px;"></p>
21+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1300-1399/1386.Cinema%20Seat%20Allocation/images/cinema_seats_3.png" style="height: 96px; width: 400px;" /></p>
2222

23-
<pre><strong>输入:</strong>n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
23+
<pre>
24+
<strong>输入:</strong>n = 3, reservedSeats = [[1,2],[1,3],[1,8],[2,6],[3,1],[3,10]]
2425
<strong>输出:</strong>4
2526
<strong>解释:</strong>上图所示是最优的安排方案,总共可以安排 4 个家庭。蓝色的叉表示被预约的座位,橙色的连续座位表示一个 4 人家庭。
2627
</pre>
2728

2829
<p><strong>示例 2:</strong></p>
2930

30-
<pre><strong>输入:</strong>n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
31+
<pre>
32+
<strong>输入:</strong>n = 2, reservedSeats = [[2,1],[1,8],[2,6]]
3133
<strong>输出:</strong>2
3234
</pre>
3335

3436
<p><strong>示例 3:</strong></p>
3537

36-
<pre><strong>输入:</strong>n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]
38+
<pre>
39+
<strong>输入:</strong>n = 4, reservedSeats = [[4,3],[1,4],[4,6],[1,7]]
3740
<strong>输出:</strong>4
3841
</pre>
3942

solution/1600-1699/1652.Defuse the Bomb/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,23 @@
6161

6262
### 方法一:模拟
6363

64-
定义答案数组 `ans`,长度为 $n$,初始时所有元素都为 $0$。根据题意,若 $k$ 为 $0$,直接返回 `ans`
64+
我们定义一个长度为 $n$ 的答案数组 $ans$,初始时所有元素都为 $0$。根据题意,若 $k$ 为 $0$,直接返回 $ans$
6565

6666
否则,遍历每个位置 $i$:
6767

6868
若 $k$ 为正数,那么 $i$ 位置的值为 $i$ 位置后 $k$ 个位置的值之和,即:
6969

7070
$$
71-
ans[i] = \sum_{j=i+1}^{i+k} code[j\mod{n}]
71+
ans[i] = \sum_{j=i+1}^{i+k} code[j \bmod n]
7272
$$
7373

7474
若 $k$ 为负数,那么 $i$ 位置的值为 $i$ 位置前 $|k|$ 个位置的值之和,即:
7575

7676
$$
77-
ans[i] = \sum_{j=i+k}^{i-1} code[(j+n)\mod{n}]
77+
ans[i] = \sum_{j=i+k}^{i-1} code[(j+n) \bmod n]
7878
$$
7979

80-
时间复杂度 $O(n\times|k|)$,忽略答案的空间消耗,空间复杂度 $O(1)$。
80+
时间复杂度 $O(n \times |k|)$,忽略答案的空间消耗,空间复杂度 $O(1)$。
8181

8282
<!-- tabs:start -->
8383

@@ -209,7 +209,7 @@ function decrypt(code: number[], k: number): number[] {
209209

210210
在方法一中,对于每个位置 $i$,都需要遍历 $k$ 个位置,有很多重复计算的操作。我们可以利用前缀和来优化。
211211

212-
我们将 `code` 数组复制一份(可以不用执行复制操作,直接通过循环遍历取模实现),得到两倍长度的数组,对其求前缀和,得到长度为 $2\times n + 1$ 的前缀和数组 $s$。
212+
我们将 `code` 数组复制一份(可以不用执行复制操作,直接通过循环遍历取模实现),得到两倍长度的数组,对其求前缀和,得到长度为 $2 \times n + 1$ 的前缀和数组 $s$。
213213

214214
若 $k$ 为正数,那么 $i$ 位置的值为 $i$ 位置后 $k$ 个位置的值之和,即 $ans[i] = s[i + k + 1] - s[i + 1]$。
215215

solution/1600-1699/1652.Defuse the Bomb/README_EN.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,25 @@
5555

5656
## Solutions
5757

58-
### Solution 1
58+
### Solution 1: Simulation
59+
60+
We define an answer array `ans` of length `n`, initially all elements are `0`. According to the problem, if `k` is `0`, return `ans` directly.
61+
62+
Otherwise, we traverse each position `i`:
63+
64+
- If `k` is a positive number, then the value at position `i` is the sum of the values at the `k` positions after position `i`, that is:
65+
66+
$$
67+
ans[i] = \sum_{j=i+1}^{i+k} code[j \bmod n]
68+
$$
69+
70+
- If `k` is a negative number, then the value at position `i` is the sum of the values at the `|k|` positions before position `i`, that is:
71+
72+
$$
73+
ans[i] = \sum_{j=i+k}^{i-1} code[(j+n) \bmod n]
74+
$$
75+
76+
The time complexity is $O(n \times |k|)$, ignoring the space consumption of the answer, the space complexity is $O(1)$.
5977

6078
<!-- tabs:start -->
6179

@@ -183,7 +201,17 @@ function decrypt(code: number[], k: number): number[] {
183201

184202
<!-- tabs:end -->
185203

186-
### Solution 2
204+
### Solution 2: Prefix Sum
205+
206+
In Solution 1, for each position $i$, we need to traverse $k$ positions, which involves a lot of repeated calculations. We can optimize this by using prefix sums.
207+
208+
We duplicate the `code` array (this can be achieved without actually duplicating the array, but by cyclically traversing with modulo operation), resulting in an array of twice the length. We then calculate the prefix sum of this array, resulting in a prefix sum array $s$ of length $2 \times n + 1$.
209+
210+
If $k$ is a positive number, then the value at position $i$ is the sum of the values at the $k$ positions after position $i$, i.e., $ans[i] = s[i + k + 1] - s[i + 1]$.
211+
212+
If $k$ is a negative number, then the value at position $i$ is the sum of the values at the $|k|$ positions before position $i$, i.e., $ans[i] = s[i + n] - s[i + k + n]$.
213+
214+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the `code` array.
187215

188216
<!-- tabs:start -->
189217

solution/1600-1699/1653.Minimum Deletions to Make String Balanced/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ $$
6565

6666
我们注意到,状态转移方程中只与前一个状态以及变量 $b$ 有关,因此我们可以仅用一个答案变量 $ans$ 维护当前的 $f[i]$,并不需要开辟数组 $f$。
6767

68-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
68+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$
6969

7070
<!-- tabs:start -->
7171

solution/1600-1699/1653.Minimum Deletions to Make String Balanced/README_EN.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,29 @@ Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u
3939

4040
## Solutions
4141

42-
### Solution 1
42+
### Solution 1: Dynamic Programming
43+
44+
We define $f[i]$ as the minimum number of characters to be deleted in the first $i$ characters to make the string balanced. Initially, $f[0]=0$. The answer is $f[n]$.
45+
46+
We traverse the string $s$, maintaining a variable $b$, which represents the number of character 'b' in the characters before the current position.
47+
48+
- If the current character is 'b', it does not affect the balance of the first $i$ characters, so $f[i]=f[i-1]$, then we update $b \leftarrow b+1$.
49+
- If the current character is 'a', we can choose to delete the current character, so $f[i]=f[i-1]+1$; or we can choose to delete the previous character 'b', so $f[i]=b$. Therefore, we take the minimum of the two, that is, $f[i]=\min(f[i-1]+1,b)$.
50+
51+
In summary, we can get the state transition equation:
52+
53+
$$
54+
f[i]=\begin{cases}
55+
f[i-1], & s[i-1]='b'\\
56+
\min(f[i-1]+1,b), & s[i-1]='a'
57+
\end{cases}
58+
$$
59+
60+
The final answer is $f[n]$.
61+
62+
We notice that the state transition equation is only related to the previous state and the variable $b$, so we can just use an answer variable $ans$ to maintain the current $f[i]$, and there is no need to allocate an array $f$.
63+
64+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
4365

4466
<!-- tabs:start -->
4567

@@ -135,7 +157,13 @@ function minimumDeletions(s: string): number {
135157

136158
<!-- tabs:end -->
137159

138-
### Solution 2
160+
### Solution 2: Enumeration + Prefix Sum
161+
162+
We can enumerate each position $i$ in the string $s$, dividing the string $s$ into two parts, namely $s[0,..,i-1]$ and $s[i+1,..n-1]$. To make the string balanced, the number of characters we need to delete at the current position $i$ is the number of character 'b' in $s[0,..,i-1]$ plus the number of character 'a' in $s[i+1,..n-1]$.
163+
164+
Therefore, we maintain two variables $lb$ and $ra$ to represent the number of character 'b' in $s[0,..,i-1]$ and the number of character 'a' in $s[i+1,..n-1]$ respectively. The number of characters we need to delete is $lb+ra$. During the enumeration process, we update the variables $lb$ and $ra$.
165+
166+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the string $s$.
139167

140168
<!-- tabs:start -->
141169

solution/1600-1699/1654.Minimum Jumps to Reach Home/README_EN.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,26 @@
5656

5757
## Solutions
5858

59-
### Solution 1
59+
### Solution 1: BFS
60+
61+
We can use the position and jumping direction of the flea as the state, and use BFS to search for the shortest path. The key point of this problem is to determine the right boundary, that is, how far the flea can jump.
62+
63+
If $a \geq b$, that is, the distance to jump forward is greater than the distance to jump backward, then if the flea is in a position greater than $x+b$, it can no longer jump forward, because the flea cannot jump backward consecutively. If it continues to jump forward, it will never be able to jump to the position $x$. Therefore, if $a \geq b$, the right boundary can be $x+b$.
64+
65+
If $a < b$, that is, the distance to jump forward is less than the distance to jump backward, then if the position of the flea minus $b$ exceeds $2000$, choose to jump backward, otherwise jump forward. Therefore, if $a < b$, the right boundary does not exceed $6000$.
66+
67+
In summary, we can set the right boundary to $6000$.
68+
69+
Next, we use BFS to search for the shortest path. We use a queue, initially adding the position and jumping direction of the flea as the state to the queue. Each time we take a state from the queue, if the position of this state is equal to $x$, then we have found a path from the initial state to the target state, and we can return the current number of steps. Otherwise, we add the next state of the current state to the queue. There are two cases for the next state:
70+
71+
- Jump forward, the jumping direction is $1$;
72+
- When the current jumping direction is $1$, jump backward, the jumping direction is $0$.
73+
74+
Note that we need to judge whether the next state is legal, that is, the position of the next state does not exceed the right boundary, is not in the forbidden position, and has not been visited.
75+
76+
If the queue is empty, it means that the target position cannot be reached, return $-1$.
77+
78+
The time complexity is $O(M)$, and the space complexity is $O(M)$. Here, $M$ is the right boundary, and in this problem, $M \leq 6000$.
6079

6180
<!-- tabs:start -->
6281

solution/1600-1699/1655.Distribute Repeating Integers/README_EN.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,21 @@
5454

5555
## Solutions
5656

57-
### Solution 1
57+
### Solution 1: State Compression Dynamic Programming + Subset Enumeration
58+
59+
First, we count the occurrence of each number in the array `nums`, and record it in the hash table `cnt`. Then we store the values in the hash table into the array `arr`. We denote the length of the array `arr` as `n`.
60+
61+
Note that the length of the array `quantity` does not exceed 10, so we can use a binary number to represent a subset of `quantity`. That is, the number `j` represents a subset of `quantity`, where the `i`-th bit of the binary representation of `j` is `1` means the `i`-th number in `quantity` is selected, and `0` means the `i`-th number is not selected.
62+
63+
We can preprocess an array `s`, where `s[j]` represents the sum of all numbers in the subset `j` of `quantity`.
64+
65+
Next, we define `f[i][j]` to represent whether the numbers in `arr[0,..i-1]` can be successfully allocated to the subset `j` of `quantity`, where `i` ranges from `[0,..n-1]`, and `j` ranges from `[0,2^m-1]`, where `m` is the length of `quantity`.
66+
67+
Considering `f[i][j]`, if there exists a subset `k` in `j` such that `s[k] <= arr[i]`, and `f[i-1][j XOR k]` is true, then `f[i][j]` is true, otherwise `f[i][j]` is false.
68+
69+
The answer is `f[n-1][2^m-1]`.
70+
71+
The time complexity is `O(n * 3^m)`, and the space complexity is `O(n * 2^m)`. Here, `n` is the number of different integers in the array `nums`, and `m` is the length of the array `quantity`.
5872

5973
<!-- tabs:start -->
6074

solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Hash Table + Prefix Sum
47+
48+
We can transform the problem into finding the maximum length of a continuous subarray in the middle, such that the sum of the subarray is $x = sum(nums) - x$.
49+
50+
Define a hash table `vis`, where `vis[s]` represents the smallest index with a prefix sum of $s$.
51+
52+
Traverse the array `nums`. For each element $nums[i]$, we first add $nums[i]$ to the prefix sum $s$. If $s$ does not exist in the hash table, we add it to the hash table, and its value is the current index $i$. Then we check whether $s - x$ exists in the hash table. If it does, it means that there exists an index $j$ such that the sum of $nums[j + 1,..i]$ is $x$. At this time, we update the minimum value of the answer, that is, $ans = min(ans, n - (i - j))$.
53+
54+
At the end of the traversal, if no subarray meets the condition, return $-1$, otherwise return $ans$.
55+
56+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array `nums`.
4757

4858
<!-- tabs:start -->
4959

@@ -218,7 +228,15 @@ int minOperations(int* nums, int numsSize, int x) {
218228
219229
<!-- tabs:end -->
220230
221-
### Solution 2
231+
### Solution 2: Two Pointers
232+
233+
Similar to Solution 1, we need to find a subarray such that the sum of the subarray is $x = sum(nums) - x$.
234+
235+
Define two pointers $j$ and $i$, initially $i = j = 0$. Then we move the pointer $i$ to the right, add $nums[i]$ to the prefix sum $s$. If $s > x$, then we move the pointer $j$ to the right in a loop, and subtract $nums[j]$ from the prefix sum $s$, until $s \le x$. If $s = x$, we can update the minimum value of the answer, that is, $ans = min(ans, n - (i - j + 1))$. Continue to move the pointer $i$ to the right, and repeat the above process.
236+
237+
Finally, if no subarray meets the condition, return $-1$, otherwise return $ans$.
238+
239+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array `nums`.
222240
223241
<!-- tabs:start -->
224242

0 commit comments

Comments
 (0)