From a17ce64462cd50eb7a89a37a6f70262d14756d06 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 22 Jul 2024 22:11:17 +0300 Subject: [PATCH 1/3] feat: add js solution to lc problem: No.1717 --- .../README.md | 32 +++++++++++++++++++ .../README_EN.md | 32 +++++++++++++++++++ .../Solution.js | 27 ++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.js diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md index 523febeecd7df..22374ad0bed8a 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md @@ -259,6 +259,38 @@ function maximumGain(s: string, x: number, y: number): number { } ``` +#### JavaScript + +```js +function maximumGain(s, x, y) { + let [a, b] = ['a', 'b']; + if (x < y) { + [x, y] = [y, x]; + [a, b] = [b, a]; + } + + let [ans, cnt1, cnt2] = [0, 0, 0]; + for (let c of s) { + if (c === a) { + cnt1++; + } else if (c === b) { + if (cnt1) { + ans += x; + cnt1--; + } else { + cnt2++; + } + } else { + ans += Math.min(cnt1, cnt2) * y; + cnt1 = 0; + cnt2 = 0; + } + } + ans += Math.min(cnt1, cnt2) * y; + return ans; +} +``` + diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md index 3bd0568e9e45d..d726b8f01a5a9 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md @@ -259,6 +259,38 @@ function maximumGain(s: string, x: number, y: number): number { } ``` +#### JavaScript + +```js +function maximumGain(s, x, y) { + let [a, b] = ['a', 'b']; + if (x < y) { + [x, y] = [y, x]; + [a, b] = [b, a]; + } + + let [ans, cnt1, cnt2] = [0, 0, 0]; + for (let c of s) { + if (c === a) { + cnt1++; + } else if (c === b) { + if (cnt1) { + ans += x; + cnt1--; + } else { + cnt2++; + } + } else { + ans += Math.min(cnt1, cnt2) * y; + cnt1 = 0; + cnt2 = 0; + } + } + ans += Math.min(cnt1, cnt2) * y; + return ans; +} +``` + diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.js b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.js new file mode 100644 index 0000000000000..9e649683d53e1 --- /dev/null +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.js @@ -0,0 +1,27 @@ +function maximumGain(s, x, y) { + let [a, b] = ['a', 'b']; + if (x < y) { + [x, y] = [y, x]; + [a, b] = [b, a]; + } + + let [ans, cnt1, cnt2] = [0, 0, 0]; + for (let c of s) { + if (c === a) { + cnt1++; + } else if (c === b) { + if (cnt1) { + ans += x; + cnt1--; + } else { + cnt2++; + } + } else { + ans += Math.min(cnt1, cnt2) * y; + cnt1 = 0; + cnt2 = 0; + } + } + ans += Math.min(cnt1, cnt2) * y; + return ans; +} From 310662dd5d41dddac4af4c2496082503474413ab Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 22 Jul 2024 22:16:07 +0300 Subject: [PATCH 2/3] feat: add 2nd ts solution to lc problem: No.1717 --- .../README.md | 44 +++++++++++++++++++ .../README_EN.md | 44 +++++++++++++++++++ .../Solution2.ts | 29 ++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.ts diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md index 22374ad0bed8a..7e30e061efb2e 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md @@ -295,4 +295,48 @@ function maximumGain(s, x, y) { + + +### Solution 2: Greedy + Stack + + + +#### TypeScript + +```ts +function maximumGain(s: string, x: number, y: number): number { + const stk: string[] = []; + const pairs: Record = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} +``` + + + + + diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md index d726b8f01a5a9..407fc6d991e62 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md @@ -295,4 +295,48 @@ function maximumGain(s, x, y) { + + +### Solution 2: Greedy + Stack + + + +#### TypeScript + +```ts +function maximumGain(s: string, x: number, y: number): number { + const stk: string[] = []; + const pairs: Record = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} +``` + + + + + diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.ts b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.ts new file mode 100644 index 0000000000000..f4ed700d88a27 --- /dev/null +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.ts @@ -0,0 +1,29 @@ +function maximumGain(s: string, x: number, y: number): number { + const stk: string[] = []; + const pairs: Record = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} From 50197de1c1276797775ac82f66d1ac96f790a955 Mon Sep 17 00:00:00 2001 From: rain84 Date: Mon, 22 Jul 2024 22:17:54 +0300 Subject: [PATCH 3/3] feat: add 2nd js solution to lc problem: No.1717 --- .../README.md | 34 +++++++++++++++++++ .../README_EN.md | 34 +++++++++++++++++++ .../Solution2.js | 29 ++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.js diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md index 7e30e061efb2e..e399609fa2ebd 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README.md @@ -335,6 +335,40 @@ function maximumGain(s: string, x: number, y: number): number { } ``` +#### JavaeScript + +```js +function maximumGain(s, x, y) { + const stk = []; + const pairs = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} +``` + diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md index 407fc6d991e62..cd7204fd75dcf 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/README_EN.md @@ -335,6 +335,40 @@ function maximumGain(s: string, x: number, y: number): number { } ``` +#### JavaeScript + +```js +function maximumGain(s, x, y) { + const stk = []; + const pairs = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +} +``` + diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.js b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.js new file mode 100644 index 0000000000000..52b060f133f78 --- /dev/null +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution2.js @@ -0,0 +1,29 @@ +function maximumGain(s, x, y) { + const stk = []; + const pairs = { a: 'b', b: 'a' }; + const pair = x > y ? ['a', 'b'] : ['b', 'a']; + let str = [...s]; + let ans = 0; + let havePairs = true; + + while (havePairs) { + for (const p of pair) { + havePairs = true; + + for (const ch of str) { + if (stk.at(-1) === p && ch === pairs[p]) { + stk.pop(); + } else stk.push(ch); + } + + if (str.length === stk.length) havePairs = false; + + const multiplier = p === 'a' ? x : y; + ans += (multiplier * (str.length - stk.length)) / 2; + str = [...stk]; + stk.length = 0; + } + } + + return ans; +}