Skip to content

Commit 808fe99

Browse files
authored
feat: update solutions to lc problems: No.2595,2596 (#4084)
1 parent 0849f4f commit 808fe99

File tree

13 files changed

+64
-25
lines changed

13 files changed

+64
-25
lines changed

solution/2300-2399/2330.Valid Palindrome IV/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tags:
4141
<strong>解释:</strong> 能让 s 变成回文,且只用了两步操作的方案如下:
4242
- 将 s[0] 变成 'b' ,得到 s = "ba" 。
4343
- 将 s[1] 变成 'b' ,得到s = "bb" 。
44-
执行两步操作让 s 变成一个回文,所以返回 true 。
44+
执行两步操作让 s 变成一个回文,所以返回 true 。
4545
</pre>
4646

4747
<p><strong>示例 3:</strong></p>
@@ -69,7 +69,7 @@ tags:
6969

7070
### 方法一:双指针
7171

72-
我们可以使用双指针 $i$ 和 $j$,分别指向字符串的头尾,然后向中间移动,统计不同字符的个数,如果不同字符的个数大于 $2$,则返回 `false`,否则返回 `true`
72+
我们可以使用双指针 $i$ 和 $j$,分别指向字符串的头尾,然后向中间移动,统计不同字符的个数,如果不同字符的个数大于 $2$,则返回 $\textit{false}$,否则返回 $\textit{true}$
7373

7474
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
7575

solution/2300-2399/2330.Valid Palindrome IV/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ Two operations could be performed to make s a palindrome so return true.
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Two Pointers
69+
70+
We can use two pointers $i$ and $j$, pointing to the beginning and end of the string, respectively, and then move towards the center, counting the number of different characters. If the number of different characters is greater than $2$, return $\textit{false}$; otherwise, return $\textit{true}$.
71+
72+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the string $s$.
6973

7074
<!-- tabs:start -->
7175

solution/2300-2399/2378.Choose Edges to Maximize Score in a Tree/README_EN.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,18 @@ Note that we cannot choose more than one edge because all edges are adjacent to
7777

7878
<!-- solution:start -->
7979

80-
### Solution 1
80+
### Solution 1: Tree DP
81+
82+
We design a function $dfs(i)$, which represents the maximum sum of the weights of selected edges in the subtree rooted at node $i$, such that no two selected edges are adjacent. This function returns two values $(a, b)$. The first value $a$ represents the sum of the weights of selected edges when the edge between the current node $i$ and its parent node is selected. The second value $b$ represents the sum of the weights of selected edges when the edge between the current node $i$ and its parent node is not selected.
83+
84+
We can observe the following for the current node $i$:
85+
86+
- If the edge between $i$ and its parent node is selected, then none of the edges between $i$ and its child nodes can be selected. In this case, the value of $a$ for the current node is the sum of the $b$ values of all its child nodes.
87+
- If the edge between $i$ and its parent node is not selected, then we can select at most one edge between $i$ and its child nodes. In this case, the value of $b$ for the current node is the sum of the $a$ values of the selected child nodes and the $b$ values of the unselected child nodes, plus the weight of the edge between $i$ and the selected child node.
88+
89+
We call the function $dfs(0)$, and the second value returned is the answer, which is the sum of the weights of selected edges when the edge between the root node and its parent node is not selected.
90+
91+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes.
8192

8293
<!-- tabs:start -->
8394

solution/2500-2599/2510.Check if There is a Path With Equal Number of 0's And 1's/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ tags:
5555

5656
<!-- solution:start -->
5757

58-
### Solution 1
58+
### Solution 1: Memoization Search
59+
60+
According to the problem description, we know that the number of 0s and 1s on the path from the top-left corner to the bottom-right corner is equal, and the total number is $m + n - 1$, which means the number of 0s and 1s are both $(m + n - 1) / 2$.
61+
62+
Therefore, we can use memoization search, starting from the top-left corner and moving right or down until reaching the bottom-right corner, to check if the number of 0s and 1s on the path is equal.
63+
64+
The time complexity is $O(m \times n \times (m + n))$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively.
5965

6066
<!-- tabs:start -->
6167

solution/2500-2599/2523.Closest Prime Numbers in Range/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ tags:
6565

6666
### 方法一:线性筛
6767

68-
对于给定的范围 $[left, right]$,我们可以使用线性筛求出所有质数,然后从小到大遍历质数,找到相邻的两个质数,其差值最小的质数对即为答案。
68+
对于给定的范围 $[\textit{left}, \textit{right}]$,我们可以使用线性筛求出所有质数,然后从小到大遍历质数,找到相邻的两个质数,其差值最小的质数对即为答案。
6969

70-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n = right$。
70+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n = \textit{right}$。
7171

7272
<!-- tabs:start -->
7373

solution/2500-2599/2523.Closest Prime Numbers in Range/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ Since 11 is smaller than 17, we return the first pair.
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Linear Sieve
74+
75+
For the given range $[\textit{left}, \textit{right}]$, we can use the linear sieve method to find all prime numbers. Then, we traverse the prime numbers in ascending order to find the pair of adjacent prime numbers with the smallest difference, which will be the answer.
76+
77+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n = \textit{right}$.
7478

7579
<!-- tabs:start -->
7680

solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ tags:
6565

6666
### 方法一:哈希表 + 滑动窗口 + 快速幂
6767

68-
我们用哈希表 `cnt` 维护窗口大小为 $k$ 的元素及其出现的次数。
68+
我们用哈希表 $\textit{cnt}$ 维护窗口大小为 $k$ 的元素及其出现的次数。
6969

7070
先算出初始窗口为 $k$ 的所有元素的分数。然后利用滑动窗口,每次加入一个元素,并移除最左边的元素,同时利用快速幂更新分数。
7171

72-
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。
72+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
7373

7474
<!-- tabs:start -->
7575

solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ tags:
6363

6464
<!-- solution:start -->
6565

66-
### Solution 1
66+
### Solution 1: Hash Table + Sliding Window + Fast Power
67+
68+
We use a hash table $\textit{cnt}$ to maintain the elements of the window of size $k$ and their frequencies.
69+
70+
First, calculate the score of all elements in the initial window of size $k$. Then, use a sliding window to add one element at a time and remove the leftmost element, while updating the score using fast power.
71+
72+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
6773

6874
<!-- tabs:start -->
6975

solution/2500-2599/2595.Number of Even and Odd Bits/README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ tags:
3232

3333
<pre><strong>输入:</strong>n = 17
3434
<strong>输出:</strong>[2,0]
35-
<strong>解释:</strong>17 的二进制形式是 10001 。
36-
下标 0 和 下标 4 对应的值为 1 。
35+
<strong>解释:</strong>17 的二进制形式是 10001 。
36+
下标 0 和 下标 4 对应的值为 1 。
3737
共有 2 个偶数下标,0 个奇数下标。
3838
</pre>
3939

4040
<p><strong>示例 2:</strong></p>
4141

4242
<pre><strong>输入:</strong>n = 2
4343
<strong>输出:</strong>[0,1]
44-
<strong>解释:</strong>2 的二进制形式是 10 。
45-
下标 1 对应的值为 1 。
44+
<strong>解释:</strong>2 的二进制形式是 10 。
45+
下标 1 对应的值为 1 。
4646
共有 0 个偶数下标,1 个奇数下标。
4747
</pre>
4848

@@ -127,7 +127,7 @@ func evenOddBit(n int) []int {
127127

128128
```ts
129129
function evenOddBit(n: number): number[] {
130-
const ans = new Array(2).fill(0);
130+
const ans = Array(2).fill(0);
131131
for (let i = 0; n > 0; n >>= 1, i ^= 1) {
132132
ans[i] += n & 1;
133133
}
@@ -161,7 +161,11 @@ impl Solution {
161161

162162
<!-- solution:start -->
163163

164-
### 方法二
164+
### 方法二:位运算
165+
166+
我们可以定义一个掩码 $\textit{mask} = \text{0x5555}$,它的二进制表示为 $\text{0101 0101 0101 0101}_2$。那么 $n$ 与 $\textit{mask}$ 进行按位与运算,就可以得到 $n$ 的二进制表示中偶数下标的位,而 $n$ 与 $\textit{mask}$ 取反后再进行按位与运算,就可以得到 $n$ 的二进制表示中奇数下标的位。统计这两个结果中 $1$ 的个数即可。
167+
168+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。其中 $n$ 为给定的整数。
165169

166170
<!-- tabs:start -->
167171

solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func evenOddBit(n int) []int {
137137

138138
```ts
139139
function evenOddBit(n: number): number[] {
140-
const ans = new Array(2).fill(0);
140+
const ans = Array(2).fill(0);
141141
for (let i = 0; n > 0; n >>= 1, i ^= 1) {
142142
ans[i] += n & 1;
143143
}
@@ -171,7 +171,11 @@ impl Solution {
171171

172172
<!-- solution:start -->
173173

174-
### Solution 2
174+
### Solution 2: Bit Manipulation
175+
176+
We can define a mask $\textit{mask} = \text{0x5555}$, which is represented in binary as $\text{0101 0101 0101 0101}_2$. Then, performing a bitwise AND operation between $n$ and $\textit{mask}$ will give us the bits at even indices in the binary representation of $n$. Performing a bitwise AND operation between $n$ and the complement of $\textit{mask}$ will give us the bits at odd indices in the binary representation of $n$. We can count the number of 1s in these two results.
177+
178+
The time complexity is $O(1)$, and the space complexity is $O(1)$. Here, $n$ is the given integer.
175179

176180
<!-- tabs:start -->
177181

solution/2500-2599/2595.Number of Even and Odd Bits/Solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function evenOddBit(n: number): number[] {
2-
const ans = new Array(2).fill(0);
2+
const ans = Array(2).fill(0);
33
for (let i = 0; n > 0; n >>= 1, i ^= 1) {
44
ans[i] += n & 1;
55
}

solution/2500-2599/2596.Check Knight Tour Configuration/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ tags:
6868

6969
### 方法一:模拟
7070

71-
我们先用数组 $pos$ 记录骑士访问的每个格子的坐标,然后遍历 $pos$ 数组,检查相邻两个格子的坐标差是否为 $(1, 2)$ 或 $(2, 1)$ 即可。若不满足,则返回 `false`
71+
我们先用数组 $\textit{pos}$ 记录骑士访问的每个格子的坐标,然后遍历 $\textit{pos}$ 数组,检查相邻两个格子的坐标差是否为 $(1, 2)$ 或 $(2, 1)$ 即可。若不满足,则返回 $\textit{false}$
7272

73-
否则遍历结束后,返回 `true`
73+
否则遍历结束后,返回 $\textit{true}$
7474

7575
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为棋盘的边长。
7676

solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ tags:
6565

6666
### Solution 1: Simulation
6767

68-
We first use the array $pos$ to record the coordinates of the grid visited by the knight, and then traverse the $pos$ array to check whether the difference between the adjacent two grid coordinates is $(1, 2)$ or $(2, 1)$. If not, return `false`.
68+
We first use an array $\textit{pos}$ to record the coordinates of each cell visited by the knight, then traverse the $\textit{pos}$ array and check if the coordinate difference between two adjacent cells is $(1, 2)$ or $(2, 1)$. If not, return $\textit{false}$.
6969

70-
Otherwise, return `true` after the traversal ends.
70+
Otherwise, after the traversal, return $\textit{true}$.
7171

72-
The time complexity is $O(n^2)$ and the space complexity is $O(n^2)$, where $n$ is the length of the chessboard.
72+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the side length of the chessboard.
7373

7474
<!-- tabs:start -->
7575

0 commit comments

Comments
 (0)