Skip to content

Commit 69c86eb

Browse files
add 1717
1 parent dca462c commit 69c86eb

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ _If you like this project, please leave me a star._ ★
1111
|1721|[Swapping Nodes in a Linked List](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1721.java) ||Medium|LinkedList|
1212
|1720|[Decode XORed Array](https://leetcode.com/problems/decode-xored-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1720.java) ||Easy|Bit Manipulation|
1313
|1718|[Construct the Lexicographically Largest Valid Sequence](https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1718.java) ||Medium|Backtracking, Recursion|
14+
|1717|[Maximum Score From Removing Substrings](https://leetcode.com/problems/maximum-score-from-removing-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1717.java) ||Medium|Greedy|
1415
|1716|[Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1716.java) ||Easy|Math, Greedy|
1516
|1711|[Count Good Meals](https://leetcode.com/problems/count-good-meals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1711.java) ||Medium|Array, HashTable, Two Pointers|
1617
|1710|[Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1710.java) ||Easy|Greedy, Sort|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Stack;
4+
5+
public class _1717 {
6+
public static class Solution1 {
7+
public int maximumGain(String s, int x, int y) {
8+
Stack<Character> stack1 = new Stack<>();
9+
int big = x > y ? x : y;
10+
int small = big == x ? y : x;
11+
char first = x == big ? 'a' : 'b';
12+
char second = first == 'a' ? 'b' : 'a';
13+
int maximumGain = 0;
14+
for (char c : s.toCharArray()) {
15+
if (c == second && !stack1.isEmpty() && stack1.peek() == first) {
16+
stack1.pop();
17+
maximumGain += big;
18+
} else {
19+
stack1.push(c);
20+
}
21+
}
22+
Stack<Character> stack2 = new Stack<>();
23+
while (!stack1.isEmpty()) {
24+
char c = stack1.pop();
25+
if (c == second && !stack2.isEmpty() && stack2.peek() == first) {
26+
stack2.pop();
27+
maximumGain += small;
28+
} else {
29+
stack2.push(c);
30+
}
31+
}
32+
return maximumGain;
33+
}
34+
}
35+
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1717;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1717Test {
10+
private static _1717.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1717.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(19, solution1.maximumGain("cdbcbbaaabab", 4, 5));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(20, solution1.maximumGain("aabbaaxybbaabb", 5, 4));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)