Movatterモバイル変換


[0]ホーム

URL:


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

Commit69c86eb

Browse files
add 1717
1 parentdca462c commit69c86eb

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Stack;
4+
5+
publicclass_1717 {
6+
publicstaticclassSolution1 {
7+
publicintmaximumGain(Strings,intx,inty) {
8+
Stack<Character>stack1 =newStack<>();
9+
intbig =x >y ?x :y;
10+
intsmall =big ==x ?y :x;
11+
charfirst =x ==big ?'a' :'b';
12+
charsecond =first =='a' ?'b' :'a';
13+
intmaximumGain =0;
14+
for (charc :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 =newStack<>();
23+
while (!stack1.isEmpty()) {
24+
charc =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+
returnmaximumGain;
33+
}
34+
}
35+
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1717;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1717Test {
10+
privatestatic_1717.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1717.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(19,solution1.maximumGain("cdbcbbaaabab",4,5));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(20,solution1.maximumGain("aabbaaxybbaabb",5,4));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp