Skip to content

Commit 06533cb

Browse files
authored
feat: add solutions to lc problems: No.2645,2696,2707 (doocs#2183)
1 parent cb593b9 commit 06533cb

File tree

9 files changed

+551
-72
lines changed

9 files changed

+551
-72
lines changed

solution/2600-2699/2645.Minimum Additions to Make Valid String/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Greedy + Two Pointers**
47+
48+
We define the string $s$ as `"abc"`, and use pointers $i$ and $j$ to point to $s$ and $word$ respectively.
49+
50+
If $word[j] \neq s[i]$, we need to insert $s[i]$, and we add $1$ to the answer; otherwise, it means that $word[j]$ can match with $s[i]$, and we move $j$ one step to the right.
51+
52+
Then, we move $i$ one step to the right, i.e., $i = (i + 1) \bmod 3$. We continue the above operations until $j$ reaches the end of the string $word$.
53+
54+
Finally, we check whether the last character of $word$ is `'b'` or `'a'`. If it is, we need to insert `'c'` or `'bc'`, and we add $1$ or $2$ to the answer and return it.
55+
56+
The time complexity is $O(n)$, where $n$ is the length of the string $word$. The space complexity is $O(1)$.
57+
4658
<!-- tabs:start -->
4759

4860
### **Python3**

solution/2600-2699/2696.Minimum String Length After Removing Substrings/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

5656
最后栈中剩余的元素个数就是最终字符串的长度。
5757

58+
> 在实现上,我们可以在栈中预先放入一个空字符,这样就不需要在遍历字符串时判断栈是否为空了,最后返回栈的大小减一即可。
59+
5860
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
5961

6062
<!-- tabs:start -->
@@ -137,9 +139,9 @@ func minLength(s string) int {
137139
function minLength(s: string): number {
138140
const stk: string[] = [''];
139141
for (const c of s) {
140-
if (c === 'B' && stk[stk.length - 1] === 'A') {
142+
if (c === 'B' && stk.at(-1)! === 'A') {
141143
stk.pop();
142-
} else if (c === 'D' && stk[stk.length - 1] === 'C') {
144+
} else if (c === 'D' && stk.at(-1)! === 'C') {
143145
stk.pop();
144146
} else {
145147
stk.push(c);

solution/2600-2699/2696.Minimum String Length After Removing Substrings/README_EN.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ It can be shown that it is the minimum length that we can obtain.</pre>
4343

4444
## Solutions
4545

46+
**Solution 1: Stack**
47+
48+
We traverse the string $s$. For the current character $c$ we are traversing, if the stack is not empty and the top element of the stack $top$ can form $AB$ or $CD$ with $c$, then we pop the top element of the stack, otherwise we push $c$ into the stack.
49+
50+
The number of remaining elements in the stack is the length of the final string.
51+
52+
> In implementation, we can pre-place an empty character in the stack, so there is no need to judge whether the stack is empty when traversing the string. Finally, we can return the size of the stack minus one.
53+
54+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
55+
4656
<!-- tabs:start -->
4757

4858
### **Python3**
@@ -119,9 +129,9 @@ func minLength(s string) int {
119129
function minLength(s: string): number {
120130
const stk: string[] = [''];
121131
for (const c of s) {
122-
if (c === 'B' && stk[stk.length - 1] === 'A') {
132+
if (c === 'B' && stk.at(-1)! === 'A') {
123133
stk.pop();
124-
} else if (c === 'D' && stk[stk.length - 1] === 'C') {
134+
} else if (c === 'D' && stk.at(-1)! === 'C') {
125135
stk.pop();
126136
} else {
127137
stk.push(c);

solution/2600-2699/2696.Minimum String Length After Removing Substrings/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function minLength(s: string): number {
22
const stk: string[] = [''];
33
for (const c of s) {
4-
if (c === 'B' && stk[stk.length - 1] === 'A') {
4+
if (c === 'B' && stk.at(-1)! === 'A') {
55
stk.pop();
6-
} else if (c === 'D' && stk[stk.length - 1] === 'C') {
6+
} else if (c === 'D' && stk.at(-1)! === 'C') {
77
stk.pop();
88
} else {
99
stk.push(c);

solution/2600-2699/2698.Find the Punishment Number of an Integer/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
4848

4949
## Solutions
5050

51+
**Solution 1: Enumeration + DFS**
52+
53+
We enumerate $i$, where $1 \leq i \leq n$. For each $i$, we split the decimal representation string of $x = i^2$, and then check whether it meets the requirements of the problem. If it does, we add $x$ to the answer.
54+
55+
After the enumeration ends, we return the answer.
56+
57+
The time complexity is $O(n^{1 + 2 \log_{10}^2})$, and the space complexity is $O(\log n)$, where $n$ is the given positive integer.
58+
5159
<!-- tabs:start -->
5260

5361
### **Python3**

solution/2600-2699/2699.Modify Graph Edge Weights/README_EN.md

+15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@
6363

6464
## Solutions
6565

66+
**Solution 1: Shortest Path (Dijkstra's Algorithm)**
67+
68+
First, we ignore the edges with a weight of $-1$ and use Dijkstra's algorithm to find the shortest distance $d$ from $source$ to $destination$.
69+
70+
- If $d < target$, it means there is a shortest path composed entirely of positive weight edges. No matter how we modify the edges with a weight of $-1$, we cannot make the shortest distance from $source$ to $destination$ equal to $target$. Therefore, there is no modification scheme that satisfies the problem, and we can return an empty array.
71+
72+
- If $d = target$, it means there is a shortest path composed entirely of positive weight edges, and its length is $target$. In this case, we can modify all edges with a weight of $-1$ to the maximum value $2 \times 10^9$.
73+
74+
- If $d > target$, we can try to add an edge with a weight of $-1$ to the graph, set the weight of the edge to $1$, and then use Dijkstra's algorithm again to find the shortest distance $d$ from $source$ to $destination$.
75+
76+
- If the shortest distance $d \leq target$, it means that after adding this edge, the shortest path can be shortened, and the shortest path must pass through this edge. Then we only need to change the weight of this edge to $target-d+1$ to make the shortest path equal to $target$. Then we can modify the remaining edges with a weight of $-1$ to the maximum value $2 \times 10^9$.
77+
- If the shortest distance $d > target$, it means that after adding this edge, the shortest path will not be shortened. Then we greedily keep the weight of this edge as $-1$ and continue to try to add the remaining edges with a weight of $-1$.
78+
79+
The time complexity is $O(n^3)$, and the space complexity is $O(n^2)$, where $n$ is the number of points in the graph.
80+
6681
<!-- tabs:start -->
6782

6883
### **Python3**

0 commit comments

Comments
 (0)