Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.2645,2696,2707 (#2183)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Jan 5, 2024
1 parent cb593b9 commit 06533cb
Show file tree
Hide file tree
Showing 9 changed files with 551 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@

## Solutions

**Solution 1: Greedy + Two Pointers**

We define the string $s$ as `"abc"`, and use pointers $i$ and $j$ to point to $s$ and $word$ respectively.

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.

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

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.

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

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

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

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

<!-- tabs:start -->
Expand Down Expand Up @@ -137,9 +139,9 @@ func minLength(s string) int {
function minLength(s: string): number {
const stk: string[] = [''];
for (const c of s) {
if (c === 'B' && stk[stk.length - 1] === 'A') {
if (c === 'B' && stk.at(-1)! === 'A') {
stk.pop();
} else if (c === 'D' && stk[stk.length - 1] === 'C') {
} else if (c === 'D' && stk.at(-1)! === 'C') {
stk.pop();
} else {
stk.push(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ It can be shown that it is the minimum length that we can obtain.</pre>

## Solutions

**Solution 1: Stack**

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.

The number of remaining elements in the stack is the length of the final string.

> 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.
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -119,9 +129,9 @@ func minLength(s string) int {
function minLength(s: string): number {
const stk: string[] = [''];
for (const c of s) {
if (c === 'B' && stk[stk.length - 1] === 'A') {
if (c === 'B' && stk.at(-1)! === 'A') {
stk.pop();
} else if (c === 'D' && stk[stk.length - 1] === 'C') {
} else if (c === 'D' && stk.at(-1)! === 'C') {
stk.pop();
} else {
stk.push(c);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function minLength(s: string): number {
const stk: string[] = [''];
for (const c of s) {
if (c === 'B' && stk[stk.length - 1] === 'A') {
if (c === 'B' && stk.at(-1)! === 'A') {
stk.pop();
} else if (c === 'D' && stk[stk.length - 1] === 'C') {
} else if (c === 'D' && stk.at(-1)! === 'C') {
stk.pop();
} else {
stk.push(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478

## Solutions

**Solution 1: Enumeration + DFS**

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.

After the enumeration ends, we return the answer.

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.

<!-- tabs:start -->

### **Python3**
Expand Down
15 changes: 15 additions & 0 deletions solution/2600-2699/2699.Modify Graph Edge Weights/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@

## Solutions

**Solution 1: Shortest Path (Dijkstra's Algorithm)**

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

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

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

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

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

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.

<!-- tabs:start -->

### **Python3**
Expand Down
Loading

0 comments on commit 06533cb

Please sign in to comment.