Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 55cf3d7

Browse files
committed
Add solution #1717
1 parent ac33e7d commit 55cf3d7

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,496 LeetCode solutions in JavaScript
1+
# 1,497 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1322,6 +1322,7 @@
13221322
1712|[Ways to Split Array Into Three Subarrays](./solutions/1712-ways-to-split-array-into-three-subarrays.js)|Medium|
13231323
1713|[Minimum Operations to Make a Subsequence](./solutions/1713-minimum-operations-to-make-a-subsequence.js)|Hard|
13241324
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
1325+
1717|[Maximum Score From Removing Substrings](./solutions/1717-maximum-score-from-removing-substrings.js)|Medium|
13251326
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13261327
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
13271328
1732|[Find the Highest Altitude](./solutions/1732-find-the-highest-altitude.js)|Easy|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* 1717. Maximum Score From Removing Substrings
3+
* https://leetcode.com/problems/maximum-score-from-removing-substrings/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string s and two integers x and y. You can perform two types of operations
7+
* any number of times.
8+
*
9+
* - Remove substring "ab" and gain x points.
10+
* - For example, when removing "ab" from "cabxbae" it becomes "cxbae".
11+
* - Remove substring "ba" and gain y points.
12+
* - For example, when removing "ba" from "cabxbae" it becomes "cabxe".
13+
*
14+
* Return the maximum points you can gain after applying the above operations on s.
15+
*/
16+
17+
/**
18+
* @param {string} s
19+
* @param {number} x
20+
* @param {number} y
21+
* @return {number}
22+
*/
23+
var maximumGain = function(s, x, y) {
24+
let result = 0;
25+
const stack = [];
26+
27+
const [primary, secondary, primaryScore, secondaryScore] = x >= y
28+
? ['ab', 'ba', x, y]
29+
: ['ba', 'ab', y, x];
30+
31+
for (const char of s) {
32+
if (stack.length && stack[stack.length - 1] === primary[0] && char === primary[1]) {
33+
stack.pop();
34+
result += primaryScore;
35+
} else {
36+
stack.push(char);
37+
}
38+
}
39+
40+
const remaining = [];
41+
for (const char of stack) {
42+
if (remaining.length && remaining[remaining.length - 1] === secondary[0]
43+
&& char === secondary[1]) {
44+
remaining.pop();
45+
result += secondaryScore;
46+
} else {
47+
remaining.push(char);
48+
}
49+
}
50+
51+
return result;
52+
};

0 commit comments

Comments
 (0)