Skip to content

feat: update solutions to lc problems: No.2595,2596 #4084

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
Feb 20, 2025
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
4 changes: 2 additions & 2 deletions solution/2300-2399/2330.Valid Palindrome IV/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tags:
<strong>解释:</strong> 能让 s 变成回文,且只用了两步操作的方案如下:
- 将 s[0] 变成 'b' ,得到 s = "ba" 。
- 将 s[1] 变成 'b' ,得到s = "bb" 。
执行两步操作让 s 变成一个回文,所以返回 true 。
执行两步操作让 s 变成一个回文,所以返回 true 。
</pre>

<p><strong>示例 3:</strong></p>
Expand Down Expand Up @@ -69,7 +69,7 @@ tags:

### 方法一:双指针

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

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

Expand Down
6 changes: 5 additions & 1 deletion solution/2300-2399/2330.Valid Palindrome IV/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ Two operations could be performed to make s a palindrome so return true.

<!-- solution:start -->

### Solution 1
### Solution 1: Two Pointers

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}$.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the string $s$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,18 @@ Note that we cannot choose more than one edge because all edges are adjacent to

<!-- solution:start -->

### Solution 1
### Solution 1: Tree DP

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.

We can observe the following for the current node $i$:

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

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes.

<!-- tabs:start -->

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

<!-- solution:start -->

### Solution 1
### Solution 1: Memoization Search

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

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.

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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ tags:

### 方法一:线性筛

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

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ Since 11 is smaller than 17, we return the first pair.

<!-- solution:start -->

### Solution 1
### Solution 1: Linear Sieve

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n = \textit{right}$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ tags:

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

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

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

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

<!-- tabs:start -->

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

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table + Sliding Window + Fast Power

We use a hash table $\textit{cnt}$ to maintain the elements of the window of size $k$ and their frequencies.

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.

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}$.

<!-- tabs:start -->

Expand Down
16 changes: 10 additions & 6 deletions solution/2500-2599/2595.Number of Even and Odd Bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ tags:

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

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

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

Expand Down Expand Up @@ -127,7 +127,7 @@ func evenOddBit(n int) []int {

```ts
function evenOddBit(n: number): number[] {
const ans = new Array(2).fill(0);
const ans = Array(2).fill(0);
for (let i = 0; n > 0; n >>= 1, i ^= 1) {
ans[i] += n & 1;
}
Expand Down Expand Up @@ -161,7 +161,11 @@ impl Solution {

<!-- solution:start -->

### 方法二
### 方法二:位运算

我们可以定义一个掩码 $\textit{mask} = \text{0x5555}$,它的二进制表示为 $\text{0101 0101 0101 0101}_2$。那么 $n$ 与 $\textit{mask}$ 进行按位与运算,就可以得到 $n$ 的二进制表示中偶数下标的位,而 $n$ 与 $\textit{mask}$ 取反后再进行按位与运算,就可以得到 $n$ 的二进制表示中奇数下标的位。统计这两个结果中 $1$ 的个数即可。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。其中 $n$ 为给定的整数。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func evenOddBit(n int) []int {

```ts
function evenOddBit(n: number): number[] {
const ans = new Array(2).fill(0);
const ans = Array(2).fill(0);
for (let i = 0; n > 0; n >>= 1, i ^= 1) {
ans[i] += n & 1;
}
Expand Down Expand Up @@ -171,7 +171,11 @@ impl Solution {

<!-- solution:start -->

### Solution 2
### Solution 2: Bit Manipulation

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.

The time complexity is $O(1)$, and the space complexity is $O(1)$. Here, $n$ is the given integer.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function evenOddBit(n: number): number[] {
const ans = new Array(2).fill(0);
const ans = Array(2).fill(0);
for (let i = 0; n > 0; n >>= 1, i ^= 1) {
ans[i] += n & 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ tags:

### 方法一:模拟

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

否则遍历结束后,返回 `true`。
否则遍历结束后,返回 $\textit{true}$

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ tags:

### Solution 1: Simulation

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`.
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}$.

Otherwise, return `true` after the traversal ends.
Otherwise, after the traversal, return $\textit{true}$.

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

<!-- tabs:start -->

Expand Down
Loading