Skip to content

Commit 12fd30b

Browse files
authored
feat: update solutions to lc problems: No.2710~2712 (#3120)
1 parent bdab969 commit 12fd30b

File tree

8 files changed

+42
-69
lines changed

8 files changed

+42
-69
lines changed

solution/2600-2699/2664.The Knight’s Tour/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ tags:
5959

6060
<!-- solution:start -->
6161

62-
### Solution 1
62+
### Solution 1: Backtracking
63+
64+
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.
65+
66+
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.
67+
68+
Finally, return the two-dimensional array $g$.
69+
70+
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.
6371

6472
<!-- tabs:start -->
6573

solution/2700-2799/2710.Remove Trailing Zeros From a String/README.md

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,30 +140,4 @@ impl Solution {
140140

141141
<!-- solution:end -->
142142

143-
<!-- solution:start -->
144-
145-
### 方法二
146-
147-
<!-- tabs:start -->
148-
149-
#### Rust
150-
151-
```rust
152-
impl Solution {
153-
pub fn remove_trailing_zeros(num: String) -> String {
154-
num.chars()
155-
.rev()
156-
.skip_while(|&c| c == '0')
157-
.collect::<String>()
158-
.chars()
159-
.rev()
160-
.collect::<String>()
161-
}
162-
}
163-
```
164-
165-
<!-- tabs:end -->
166-
167-
<!-- solution:end -->
168-
169143
<!-- problem:end -->

solution/2700-2799/2710.Remove Trailing Zeros From a String/README_EN.md

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ tags:
5252

5353
<!-- solution:start -->
5454

55-
### Solution 1
55+
### Solution 1: Traversal
56+
57+
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.
58+
59+
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)$.
5660

5761
<!-- tabs:start -->
5862

@@ -136,30 +140,4 @@ impl Solution {
136140

137141
<!-- solution:end -->
138142

139-
<!-- solution:start -->
140-
141-
### Solution 2
142-
143-
<!-- tabs:start -->
144-
145-
#### Rust
146-
147-
```rust
148-
impl Solution {
149-
pub fn remove_trailing_zeros(num: String) -> String {
150-
num.chars()
151-
.rev()
152-
.skip_while(|&c| c == '0')
153-
.collect::<String>()
154-
.chars()
155-
.rev()
156-
.collect::<String>()
157-
}
158-
}
159-
```
160-
161-
<!-- tabs:end -->
162-
163-
<!-- solution:end -->
164-
165143
<!-- problem:end -->

solution/2700-2799/2710.Remove Trailing Zeros From a String/Solution2.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

solution/2700-2799/2711.Difference of Number of Distinct Values on Diagonals/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ tags:
7878

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

81-
### 方法一
81+
### 方法一:模拟
82+
83+
我们可以按照题目描述的流程模拟,计算出每个单元格的左上角对角线上不同值的数量 $tl$ 和右下角对角线上不同值的数量 $br$,然后计算它们的差值 $|tl - br|$。
84+
85+
时间复杂度 $O(m \times n \times \min(m, n))$,空间复杂度 $O(m \times n)$。
8286

8387
<!-- tabs:start -->
8488

solution/2700-2799/2711.Difference of Number of Distinct Values on Diagonals/README_EN.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ tags:
170170

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

173-
### Solution 1
173+
### Solution 1: Simulation
174+
175+
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|$.
176+
177+
The time complexity is $O(m \times n \times \min(m, n))$, and the space complexity is $O(m \times n)$.
174178

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

solution/2700-2799/2712.Minimum Cost to Make All Characters Equal/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ tags:
6868

6969
<!-- solution:start -->
7070

71-
### 方法一
71+
### 方法一:贪心
72+
73+
根据题目描述,如果 $s[i] \neq s[i - 1]$,那么一定要执行操作,否则无法使所有字符相等。
74+
75+
我们要么选择将 $s[0..i-1]$ 的字符全部反转,反转的成本为 $i$;要么选择将 $s[i..n-1]$ 的字符全部反转,反转的成本为 $n - i$。取两者中的最小值即可。
76+
77+
我们遍历字符串 $s$,将所有需要反转的字符的成本相加,即可得到最小成本。
78+
79+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。
7280

7381
<!-- tabs:start -->
7482

solution/2700-2799/2712.Minimum Cost to Make All Characters Equal/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ The total cost to make all characters equal is 9. It can be shown that 9 is the
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Greedy Algorithm
71+
72+
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.
73+
74+
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.
75+
76+
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.
77+
78+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
7179

7280
<!-- tabs:start -->
7381

0 commit comments

Comments
 (0)