Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 445cbf5

Browse files
authored
feat: add 2nd js solution to lc problem: No.1717 (doocs#3309)
1 parent 6cfdcc7 commit 445cbf5

File tree

5 files changed

+305
-0
lines changed

5 files changed

+305
-0
lines changed

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,116 @@ function maximumGain(s: string, x: number, y: number): number {
259259
}
260260
```
261261

262+
#### JavaScript
263+
264+
```js
265+
function maximumGain(s, x, y) {
266+
let [a, b] = ['a', 'b'];
267+
if (x < y) {
268+
[x, y] = [y, x];
269+
[a, b] = [b, a];
270+
}
271+
272+
let [ans, cnt1, cnt2] = [0, 0, 0];
273+
for (let c of s) {
274+
if (c === a) {
275+
cnt1++;
276+
} else if (c === b) {
277+
if (cnt1) {
278+
ans += x;
279+
cnt1--;
280+
} else {
281+
cnt2++;
282+
}
283+
} else {
284+
ans += Math.min(cnt1, cnt2) * y;
285+
cnt1 = 0;
286+
cnt2 = 0;
287+
}
288+
}
289+
ans += Math.min(cnt1, cnt2) * y;
290+
return ans;
291+
}
292+
```
293+
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+
}
324+
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;
331+
}
332+
}
333+
334+
return ans;
335+
}
336+
```
337+
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);
357+
}
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;
365+
}
366+
}
367+
368+
return ans;
369+
}
370+
```
371+
262372
<!-- tabs:end -->
263373

264374
<!-- solution:end -->

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,116 @@ function maximumGain(s: string, x: number, y: number): number {
259259
}
260260
```
261261

262+
#### JavaScript
263+
264+
```js
265+
function maximumGain(s, x, y) {
266+
let [a, b] = ['a', 'b'];
267+
if (x < y) {
268+
[x, y] = [y, x];
269+
[a, b] = [b, a];
270+
}
271+
272+
let [ans, cnt1, cnt2] = [0, 0, 0];
273+
for (let c of s) {
274+
if (c === a) {
275+
cnt1++;
276+
} else if (c === b) {
277+
if (cnt1) {
278+
ans += x;
279+
cnt1--;
280+
} else {
281+
cnt2++;
282+
}
283+
} else {
284+
ans += Math.min(cnt1, cnt2) * y;
285+
cnt1 = 0;
286+
cnt2 = 0;
287+
}
288+
}
289+
ans += Math.min(cnt1, cnt2) * y;
290+
return ans;
291+
}
292+
```
293+
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+
}
324+
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;
331+
}
332+
}
333+
334+
return ans;
335+
}
336+
```
337+
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);
357+
}
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;
365+
}
366+
}
367+
368+
return ans;
369+
}
370+
```
371+
262372
<!-- tabs:end -->
263373

264374
<!-- solution:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function maximumGain(s, x, y) {
2+
let [a, b] = ['a', 'b'];
3+
if (x < y) {
4+
[x, y] = [y, x];
5+
[a, b] = [b, a];
6+
}
7+
8+
let [ans, cnt1, cnt2] = [0, 0, 0];
9+
for (let c of s) {
10+
if (c === a) {
11+
cnt1++;
12+
} else if (c === b) {
13+
if (cnt1) {
14+
ans += x;
15+
cnt1--;
16+
} else {
17+
cnt2++;
18+
}
19+
} else {
20+
ans += Math.min(cnt1, cnt2) * y;
21+
cnt1 = 0;
22+
cnt2 = 0;
23+
}
24+
}
25+
ans += Math.min(cnt1, cnt2) * y;
26+
return ans;
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function maximumGain(s, x, y) {
2+
const stk = [];
3+
const pairs = { a: 'b', b: 'a' };
4+
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
5+
let str = [...s];
6+
let ans = 0;
7+
let havePairs = true;
8+
9+
while (havePairs) {
10+
for (const p of pair) {
11+
havePairs = true;
12+
13+
for (const ch of str) {
14+
if (stk.at(-1) === p && ch === pairs[p]) {
15+
stk.pop();
16+
} else stk.push(ch);
17+
}
18+
19+
if (str.length === stk.length) havePairs = false;
20+
21+
const multiplier = p === 'a' ? x : y;
22+
ans += (multiplier * (str.length - stk.length)) / 2;
23+
str = [...stk];
24+
stk.length = 0;
25+
}
26+
}
27+
28+
return ans;
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function maximumGain(s: string, x: number, y: number): number {
2+
const stk: string[] = [];
3+
const pairs: Record<string, string> = { a: 'b', b: 'a' };
4+
const pair = x > y ? ['a', 'b'] : ['b', 'a'];
5+
let str = [...s];
6+
let ans = 0;
7+
let havePairs = true;
8+
9+
while (havePairs) {
10+
for (const p of pair) {
11+
havePairs = true;
12+
13+
for (const ch of str) {
14+
if (stk.at(-1) === p && ch === pairs[p]) {
15+
stk.pop();
16+
} else stk.push(ch);
17+
}
18+
19+
if (str.length === stk.length) havePairs = false;
20+
21+
const multiplier = p === 'a' ? x : y;
22+
ans += (multiplier * (str.length - stk.length)) / 2;
23+
str = [...stk];
24+
stk.length = 0;
25+
}
26+
}
27+
28+
return ans;
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /