Skip to content

Commit cea5ac3

Browse files
committed
feat: add solutions to lc problems: No.1749,1750
* No.1749.Maximum Absolute Sum of Any Subarray * No.1750.Minimum Length of String After Deleting Similar Ends
1 parent b9c774d commit cea5ac3

File tree

6 files changed

+96
-33
lines changed

6 files changed

+96
-33
lines changed

solution/1700-1799/1749.Maximum Absolute Sum of Any Subarray/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ func min(a, b int) int {
155155
}
156156
```
157157

158+
### **TypeScript**
159+
160+
```ts
161+
function maxAbsoluteSum(nums: number[]): number {
162+
let f = 0;
163+
let g = 0;
164+
let ans = 0;
165+
for (const x of nums) {
166+
f = Math.max(f, 0) + x;
167+
g = Math.min(g, 0) + x;
168+
ans = Math.max(ans, f, -g);
169+
}
170+
return ans;
171+
}
172+
```
173+
158174
### **...**
159175

160176
```

solution/1700-1799/1749.Maximum Absolute Sum of Any Subarray/README_EN.md

+33
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@
4242

4343
## Solutions
4444

45+
**Approach 1: Dynamic Programming**
46+
47+
We define $f[i]$ to represent the maximum value of the subarray ending with $nums[i]$, and define $g[i]$ to represent the minimum value of the subarray ending with $nums[i]$. Then the state transition equation of $f[i]$ and $g[i]$ is as follows:
48+
49+
$$
50+
\begin{aligned}
51+
f[i] &= \max(f[i - 1], 0) + nums[i] \\
52+
g[i] &= \min(g[i - 1], 0) + nums[i]
53+
\end{aligned}
54+
$$
55+
56+
The final answer is the maximum value of $max(f[i], |g[i]|)$.
57+
58+
Since $f[i]$ and $g[i]$ are only related to $f[i - 1]$ and $g[i - 1]$, we can use two variables to replace the array, reducing the space complexity to $O(1)$.
59+
60+
Time complexity $O(n)$, space complexity $O(1)$, where $n$ is the length of the array $nums$.
61+
4562
<!-- tabs:start -->
4663

4764
### **Python3**
@@ -128,6 +145,22 @@ func min(a, b int) int {
128145
}
129146
```
130147

148+
### **TypeScript**
149+
150+
```ts
151+
function maxAbsoluteSum(nums: number[]): number {
152+
let f = 0;
153+
let g = 0;
154+
let ans = 0;
155+
for (const x of nums) {
156+
f = Math.max(f, 0) + x;
157+
g = Math.min(g, 0) + x;
158+
ans = Math.max(ans, f, -g);
159+
}
160+
return ans;
161+
}
162+
```
163+
131164
### **...**
132165

133166
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function maxAbsoluteSum(nums: number[]): number {
2+
let f = 0;
3+
let g = 0;
4+
let ans = 0;
5+
for (const x of nums) {
6+
f = Math.max(f, 0) + x;
7+
g = Math.min(g, 0) + x;
8+
ans = Math.max(ans, f, -g);
9+
}
10+
return ans;
11+
}

solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,19 @@ func max(a, b int) int {
161161

162162
```ts
163163
function minimumLength(s: string): number {
164-
const n = s.length;
165-
let start = 0;
166-
let end = n - 1;
167-
while (start < end && s[start] === s[end]) {
168-
while (start + 1 < end && s[start] === s[start + 1]) {
169-
start++;
164+
let i = 0;
165+
let j = s.length - 1;
166+
while (i < j && s[i] === s[j]) {
167+
while (i + 1 < j && s[i + 1] === s[i]) {
168+
++i;
170169
}
171-
while (start < end - 1 && s[end] === s[end - 1]) {
172-
end--;
170+
while (i < j - 1 && s[j - 1] === s[j]) {
171+
--j;
173172
}
174-
start++;
175-
end--;
173+
++i;
174+
--j;
176175
}
177-
return Math.max(0, end - start + 1);
176+
return Math.max(0, j - i + 1);
178177
}
179178
```
180179

solution/1700-1799/1750.Minimum Length of String After Deleting Similar Ends/README_EN.md

+16-11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656

5757
## Solutions
5858

59+
**Approach 1: Two pointers**
60+
61+
We define two pointers $i$ and $j$ to point to the head and tail of the string $s$ respectively, then move them to the middle until the characters pointed to by $i$ and $j$ are not equal, then $\max(0, j - i + 1)$ is the answer.
62+
63+
The time complexity is $O(n)$ and the space complexity is $O(1)$. Where $n$ is the length of the string $s$.
64+
5965
<!-- tabs:start -->
6066

6167
### **Python3**
@@ -145,20 +151,19 @@ func max(a, b int) int {
145151

146152
```ts
147153
function minimumLength(s: string): number {
148-
const n = s.length;
149-
let start = 0;
150-
let end = n - 1;
151-
while (start < end && s[start] === s[end]) {
152-
while (start + 1 < end && s[start] === s[start + 1]) {
153-
start++;
154+
let i = 0;
155+
let j = s.length - 1;
156+
while (i < j && s[i] === s[j]) {
157+
while (i + 1 < j && s[i + 1] === s[i]) {
158+
++i;
154159
}
155-
while (start < end - 1 && s[end] === s[end - 1]) {
156-
end--;
160+
while (i < j - 1 && s[j - 1] === s[j]) {
161+
--j;
157162
}
158-
start++;
159-
end--;
163+
++i;
164+
--j;
160165
}
161-
return Math.max(0, end - start + 1);
166+
return Math.max(0, j - i + 1);
162167
}
163168
```
164169

Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
function minimumLength(s: string): number {
2-
const n = s.length;
3-
let start = 0;
4-
let end = n - 1;
5-
while (start < end && s[start] === s[end]) {
6-
while (start + 1 < end && s[start] === s[start + 1]) {
7-
start++;
2+
let i = 0;
3+
let j = s.length - 1;
4+
while (i < j && s[i] === s[j]) {
5+
while (i + 1 < j && s[i + 1] === s[i]) {
6+
++i;
87
}
9-
while (start < end - 1 && s[end] === s[end - 1]) {
10-
end--;
8+
while (i < j - 1 && s[j - 1] === s[j]) {
9+
--j;
1110
}
12-
start++;
13-
end--;
11+
++i;
12+
--j;
1413
}
15-
return Math.max(0, end - start + 1);
14+
return Math.max(0, j - i + 1);
1615
}

0 commit comments

Comments
 (0)