Skip to content

Commit c24de6c

Browse files
Merge branch 'doocs:main' into main
2 parents 54aa98d + 65f3542 commit c24de6c

File tree

34 files changed

+32731
-32382
lines changed

34 files changed

+32731
-32382
lines changed

images/starcharts.svg

Lines changed: 32248 additions & 32145 deletions
Loading

solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md

Lines changed: 66 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ tags:
8888

8989
- 如果 $c$ 是 "a",由于要先删除 "ab",因此此时我们不消除该字符,只增加 $\textit{cnt1}$;
9090
- 如果 $c$ 是 "b",如果此时 $\textit{cnt1} > 0$,我们可以消除一个 "ab",并增加 $x$ 分,否则我们只能增加 $\textit{cnt2}$;
91-
- 如果 $c$ 是其他字符,那么对于该子字符串,我们剩下了一个 $\textit{cnt2}$ 个 "b" 和 $\textit{cnt1}$ 个 "a",我们可以消除 $\min(\textit{cnt1}, \textit{cnt2})$ 个 "ab",并增加 $y$ 分。
91+
- 如果 $c$ 是其他字符,那么对于该子字符串,我们剩下了 $\textit{cnt2}$ 个 "b" 和 $\textit{cnt1}$ 个 "a",我们可以消除 $\min(\textit{cnt1}, \textit{cnt2})$ 个 "ba",并增加若干个 $y$ 分。
9292

93-
遍历结束后,我们还需要额外处理一下剩余的 "ab",增加若干个 $y$ 分。
93+
遍历结束后,我们还需要额外处理一下剩余的 "ba",增加若干个 $y$ 分。
9494

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

@@ -259,6 +259,44 @@ function maximumGain(s: string, x: number, y: number): number {
259259
}
260260
```
261261

262+
#### Rust
263+
264+
```rust
265+
impl Solution {
266+
pub fn maximum_gain(s: String, mut x: i32, mut y: i32) -> i32 {
267+
let (mut a, mut b) = ('a', 'b');
268+
if x < y {
269+
std::mem::swap(&mut x, &mut y);
270+
std::mem::swap(&mut a, &mut b);
271+
}
272+
273+
let mut ans = 0;
274+
let mut cnt1 = 0;
275+
let mut cnt2 = 0;
276+
277+
for c in s.chars() {
278+
if c == a {
279+
cnt1 += 1;
280+
} else if c == b {
281+
if cnt1 > 0 {
282+
ans += x;
283+
cnt1 -= 1;
284+
} else {
285+
cnt2 += 1;
286+
}
287+
} else {
288+
ans += cnt1.min(cnt2) * y;
289+
cnt1 = 0;
290+
cnt2 = 0;
291+
}
292+
}
293+
294+
ans += cnt1.min(cnt2) * y;
295+
ans
296+
}
297+
}
298+
```
299+
262300
#### JavaScript
263301

264302
```js
@@ -291,81 +329,38 @@ function maximumGain(s, x, y) {
291329
}
292330
```
293331

294-
<!-- tabs:end -->
295-
296-
<!-- solution:end -->
297-
298-
<!-- solution:start -->
299-
300-
### Solution 2: Greedy + Stack
301-
302-
<!-- tabs:start -->
303-
304-
#### TypeScript
305-
306-
```ts
307-
function maximumGain(s: string, x: number, y: number): number {
308-
const stk: string[] = [];
309-
const pairs: Record<string, string> = { a: 'b', b: 'a' };
310-
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
311-
let str = [...s];
312-
let ans = 0;
313-
let havePairs = true;
314-
315-
while (havePairs) {
316-
for (const p of pair) {
317-
havePairs = true;
318-
319-
for (const ch of str) {
320-
if (stk.at(-1) === p && ch === pairs[p]) {
321-
stk.pop();
322-
} else stk.push(ch);
323-
}
332+
#### C#
324333

325-
if (str.length === stk.length) havePairs = false;
326-
327-
const multiplier = p === 'a' ? x : y;
328-
ans += (multiplier * (str.length - stk.length)) / 2;
329-
str = [...stk];
330-
stk.length = 0;
334+
```cs
335+
public class Solution {
336+
public int MaximumGain(string s, int x, int y) {
337+
char a = 'a', b = 'b';
338+
if (x < y) {
339+
(x, y) = (y, x);
340+
(a, b) = (b, a);
331341
}
332-
}
333-
334-
return ans;
335-
}
336-
```
337342

338-
#### JavaeScript
339-
340-
```js
341-
function maximumGain(s, x, y) {
342-
const stk = [];
343-
const pairs = { a: 'b', b: 'a' };
344-
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
345-
let str = [...s];
346-
let ans = 0;
347-
let havePairs = true;
348-
349-
while (havePairs) {
350-
for (const p of pair) {
351-
havePairs = true;
352-
353-
for (const ch of str) {
354-
if (stk.at(-1) === p && ch === pairs[p]) {
355-
stk.pop();
356-
} else stk.push(ch);
343+
int ans = 0, cnt1 = 0, cnt2 = 0;
344+
foreach (char c in s) {
345+
if (c == a) {
346+
cnt1++;
347+
} else if (c == b) {
348+
if (cnt1 > 0) {
349+
ans += x;
350+
cnt1--;
351+
} else {
352+
cnt2++;
353+
}
354+
} else {
355+
ans += Math.Min(cnt1, cnt2) * y;
356+
cnt1 = 0;
357+
cnt2 = 0;
357358
}
358-
359-
if (str.length === stk.length) havePairs = false;
360-
361-
const multiplier = p === 'a' ? x : y;
362-
ans += (multiplier * (str.length - stk.length)) / 2;
363-
str = [...stk];
364-
stk.length = 0;
365359
}
366-
}
367360

368-
return ans;
361+
ans += Math.Min(cnt1, cnt2) * y;
362+
return ans;
363+
}
369364
}
370365
```
371366

solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md

Lines changed: 74 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,23 @@ Total score = 5 + 4 + 5 + 5 = 19.</pre>
7676

7777
### Solution 1: Greedy
7878

79-
Let's assume that the score of the substring "ab" is always not lower than the score of the substring "ba". If not, we can swap "a" and "b", and simultaneously swap $x$ and $y$.
79+
We can assume that the score of substring "ab" is always no less than the score of substring "ba". If not, we can swap "a" and "b", and simultaneously swap $x$ and $y$.
8080

81-
Next, we only need to consider the case where the string contains only "a" and "b". If the string contains other characters, we can treat them as a dividing point, splitting the string into several substrings that contain only "a" and "b", and then calculate the score for each substring separately.
81+
Next, we only need to consider the case where the string contains only "a" and "b". If the string contains other characters, we can treat them as split points, dividing the string into several substrings that contain only "a" and "b", and then calculate the score for each substring separately.
8282

83-
We observe that, for a substring containing only "a" and "b", no matter what operations are taken, in the end, there will only be one type of character left, or an empty string. Since each operation will delete one "a" and one "b" simultaneously, the total number of operations is fixed. We can greedily delete "ab" first, then "ba", to ensure the maximum score.
83+
We observe that for a substring containing only "a" and "b", no matter what operations we take, we will eventually be left with only one type of character, or an empty string. Since each operation removes one "a" and one "b" simultaneously, the total number of operations is fixed. We can greedily remove "ab" first, then remove "ba", which ensures the maximum score.
8484

85-
Therefore, we can use two variables $\textit{cnt1}$ and $\textit{cnt2}$ to record the number of "a" and "b", respectively. Then, we traverse the string, update $\textit{cnt1}$ and $\textit{cnt2}$ based on the current character, and calculate the score.
85+
Therefore, we can use two variables $\textit{cnt1}$ and $\textit{cnt2}$ to record the counts of "a" and "b" respectively, then traverse the string and update $\textit{cnt1}$ and $\textit{cnt2}$ according to different cases of the current character, while calculating the score.
8686

87-
For the current character $c$:
87+
For the current character $c$ being traversed:
8888

89-
- If $c$ is "a", since we need to delete "ab" first, we do not eliminate this character at this time, only increase $\textit{cnt1}$;
90-
- If $c$ is "b", if $\textit{cnt1} > 0$ at this time, we can eliminate an "ab" and add $x$ points; otherwise, we can only increase $\textit{cnt2}$;
91-
- If $c$ is another character, then for this substring, we are left with $\textit{cnt2}$ "b" and $\textit{cnt1}$ "a", we can eliminate $\min(\textit{cnt1}, \textit{cnt2})$ "ab" and add $y$ points.
89+
- If $c$ is "a", since we want to remove "ab" first, we don't eliminate this character at this time, only increment $\textit{cnt1}$;
90+
- If $c$ is "b", if $\textit{cnt1} > 0$ at this time, we can eliminate one "ab" and add $x$ points; otherwise, we can only increment $\textit{cnt2}$;
91+
- If $c$ is another character, then for this substring, we have $\textit{cnt2}$ "b"s and $\textit{cnt1}$ "a"s left. We can eliminate $\min(\textit{cnt1}, \textit{cnt2})$ "ba"s and add several $y$ points.
9292

93-
After the traversal is finished, we also need to additionally handle the remaining "ab", adding several $y$ points.
93+
After traversal, we need to handle the remaining "ba"s and add several $y$ points.
9494

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

9797
<!-- tabs:start -->
9898

@@ -259,6 +259,44 @@ function maximumGain(s: string, x: number, y: number): number {
259259
}
260260
```
261261

262+
#### Rust
263+
264+
```rust
265+
impl Solution {
266+
pub fn maximum_gain(s: String, mut x: i32, mut y: i32) -> i32 {
267+
let (mut a, mut b) = ('a', 'b');
268+
if x < y {
269+
std::mem::swap(&mut x, &mut y);
270+
std::mem::swap(&mut a, &mut b);
271+
}
272+
273+
let mut ans = 0;
274+
let mut cnt1 = 0;
275+
let mut cnt2 = 0;
276+
277+
for c in s.chars() {
278+
if c == a {
279+
cnt1 += 1;
280+
} else if c == b {
281+
if cnt1 > 0 {
282+
ans += x;
283+
cnt1 -= 1;
284+
} else {
285+
cnt2 += 1;
286+
}
287+
} else {
288+
ans += cnt1.min(cnt2) * y;
289+
cnt1 = 0;
290+
cnt2 = 0;
291+
}
292+
}
293+
294+
ans += cnt1.min(cnt2) * y;
295+
ans
296+
}
297+
}
298+
```
299+
262300
#### JavaScript
263301

264302
```js
@@ -291,81 +329,38 @@ function maximumGain(s, x, y) {
291329
}
292330
```
293331

294-
<!-- tabs:end -->
295-
296-
<!-- solution:end -->
297-
298-
<!-- solution:start -->
299-
300-
### Solution 2: Greedy + Stack
301-
302-
<!-- tabs:start -->
303-
304-
#### TypeScript
305-
306-
```ts
307-
function maximumGain(s: string, x: number, y: number): number {
308-
const stk: string[] = [];
309-
const pairs: Record<string, string> = { a: 'b', b: 'a' };
310-
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
311-
let str = [...s];
312-
let ans = 0;
313-
let havePairs = true;
314-
315-
while (havePairs) {
316-
for (const p of pair) {
317-
havePairs = true;
318-
319-
for (const ch of str) {
320-
if (stk.at(-1) === p && ch === pairs[p]) {
321-
stk.pop();
322-
} else stk.push(ch);
323-
}
332+
#### C#
324333

325-
if (str.length === stk.length) havePairs = false;
326-
327-
const multiplier = p === 'a' ? x : y;
328-
ans += (multiplier * (str.length - stk.length)) / 2;
329-
str = [...stk];
330-
stk.length = 0;
334+
```cs
335+
public class Solution {
336+
public int MaximumGain(string s, int x, int y) {
337+
char a = 'a', b = 'b';
338+
if (x < y) {
339+
(x, y) = (y, x);
340+
(a, b) = (b, a);
331341
}
332-
}
333-
334-
return ans;
335-
}
336-
```
337342

338-
#### JavaeScript
339-
340-
```js
341-
function maximumGain(s, x, y) {
342-
const stk = [];
343-
const pairs = { a: 'b', b: 'a' };
344-
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
345-
let str = [...s];
346-
let ans = 0;
347-
let havePairs = true;
348-
349-
while (havePairs) {
350-
for (const p of pair) {
351-
havePairs = true;
352-
353-
for (const ch of str) {
354-
if (stk.at(-1) === p && ch === pairs[p]) {
355-
stk.pop();
356-
} else stk.push(ch);
343+
int ans = 0, cnt1 = 0, cnt2 = 0;
344+
foreach (char c in s) {
345+
if (c == a) {
346+
cnt1++;
347+
} else if (c == b) {
348+
if (cnt1 > 0) {
349+
ans += x;
350+
cnt1--;
351+
} else {
352+
cnt2++;
353+
}
354+
} else {
355+
ans += Math.Min(cnt1, cnt2) * y;
356+
cnt1 = 0;
357+
cnt2 = 0;
357358
}
358-
359-
if (str.length === stk.length) havePairs = false;
360-
361-
const multiplier = p === 'a' ? x : y;
362-
ans += (multiplier * (str.length - stk.length)) / 2;
363-
str = [...stk];
364-
stk.length = 0;
365359
}
366-
}
367360

368-
return ans;
361+
ans += Math.Min(cnt1, cnt2) * y;
362+
return ans;
363+
}
369364
}
370365
```
371366

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Solution {
2+
public int MaximumGain(string s, int x, int y) {
3+
char a = 'a', b = 'b';
4+
if (x < y) {
5+
(x, y) = (y, x);
6+
(a, b) = (b, a);
7+
}
8+
9+
int ans = 0, cnt1 = 0, cnt2 = 0;
10+
foreach (char c in s) {
11+
if (c == a) {
12+
cnt1++;
13+
} else if (c == b) {
14+
if (cnt1 > 0) {
15+
ans += x;
16+
cnt1--;
17+
} else {
18+
cnt2++;
19+
}
20+
} else {
21+
ans += Math.Min(cnt1, cnt2) * y;
22+
cnt1 = 0;
23+
cnt2 = 0;
24+
}
25+
}
26+
27+
ans += Math.Min(cnt1, cnt2) * y;
28+
return ans;
29+
}
30+
}

0 commit comments

Comments
 (0)