Skip to content
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

feat: update solutions to lc problems: No.2710~2712 #3120

Merged
merged 1 commit into from
Jun 18, 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
10 changes: 9 additions & 1 deletion solution/2600-2699/2664.The Knight’s Tour/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Backtracking

We create a two-dimensional array $g$, used to record the knight's movement order, initially $g[r][c] = -1$, and all other positions are set to $-1$ as well. Additionally, we need a variable $ok$ to record whether a solution has been found.

Next, we start depth-first search from $(r, c)$. Each time we search position $(i, j)$, we first check if $g[i][j]$ equals $m \times n - 1$. If so, it means we have found a solution, then we set $ok$ to `true` and return. Otherwise, we enumerate the knight's eight possible movement directions to position $(x, y)$. If $0 \leq x < m$, $0 \leq y < n$, and $g[x][y]=-1$, then we update $g[x][y]$ to $g[i][j]+1$, and recursively search position $(x, y)$. If after the search, the variable $ok$ is `true`, we return directly. Otherwise, we reset $g[x][y]$ to $-1$ and continue searching in other directions.

Finally, return the two-dimensional array $g$.

The time complexity is $O(8^{m \times n})$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ are the integers given in the problem.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,30 +140,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Rust

```rust
impl Solution {
pub fn remove_trailing_zeros(num: String) -> String {
num.chars()
.rev()
.skip_while(|&c| c == '0')
.collect::<String>()
.chars()
.rev()
.collect::<String>()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Traversal

We can traverse the string from the end to the beginning, stopping when we encounter the first character that is not `0`. Then, we return the substring from the beginning to this character.

The time complexity is $O(n)$, where $n$ is the length of the string. Ignoring the space consumed by the answer string, the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -136,30 +140,4 @@ impl Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Rust

```rust
impl Solution {
pub fn remove_trailing_zeros(num: String) -> String {
num.chars()
.rev()
.skip_while(|&c| c == '0')
.collect::<String>()
.chars()
.rev()
.collect::<String>()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们可以按照题目描述的流程模拟,计算出每个单元格的左上角对角线上不同值的数量 $tl$ 和右下角对角线上不同值的数量 $br$,然后计算它们的差值 $|tl - br|$。

时间复杂度 $O(m \times n \times \min(m, n))$,空间复杂度 $O(m \times n)$。

<!-- tabs:start -->

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

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

We can simulate the process described in the problem statement, calculating the number of distinct values on the top-left diagonal $tl$ and the bottom-right diagonal $br$ for each cell, then compute their difference $|tl - br|$.

The time complexity is $O(m \times n \times \min(m, n))$, and the space complexity is $O(m \times n)$.

<!-- tabs:start -->

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

<!-- solution:start -->

### 方法一
### 方法一:贪心

根据题目描述,如果 $s[i] \neq s[i - 1]$,那么一定要执行操作,否则无法使所有字符相等。

我们要么选择将 $s[0..i-1]$ 的字符全部反转,反转的成本为 $i$;要么选择将 $s[i..n-1]$ 的字符全部反转,反转的成本为 $n - i$。取两者中的最小值即可。

我们遍历字符串 $s$,将所有需要反转的字符的成本相加,即可得到最小成本。

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ The total cost to make all characters equal is 9. It can be shown that 9 is the

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy Algorithm

According to the problem description, if $s[i] \neq s[i - 1]$, an operation must be performed; otherwise, it's impossible to make all characters equal.

We can either choose to reverse all characters from $s[0..i-1]$, with a cost of $i$, or reverse all characters from $s[i..n-1]$, with a cost of $n - i$. We take the minimum of the two.

By iterating through the string $s$ and summing up the costs of all characters that need to be reversed, we can obtain the minimum cost.

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

<!-- tabs:start -->

Expand Down
Loading