Skip to content

Commit ec4af0c

Browse files
update 1717
1 parent 1f4751f commit ec4af0c

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/main/java/com/fishercoder/solutions/secondthousand/_1717.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
package com.fishercoder.solutions.secondthousand;
22

3-
import java.util.Stack;
3+
import java.util.Deque;
4+
import java.util.LinkedList;
45

56
public class _1717 {
67
public static class Solution1 {
78
public int maximumGain(String s, int x, int y) {
8-
Stack<Character> stack1 = new Stack<>();
9-
int big = x > y ? x : y;
9+
int big = Math.max(x, y);
1010
int small = big == x ? y : x;
11-
char first = x == big ? 'a' : 'b';
11+
char first = big == x ? 'a' : 'b';
1212
char second = first == 'a' ? 'b' : 'a';
13-
int maximumGain = 0;
13+
Deque<Character> stack1 = new LinkedList<>();
14+
int max = 0;
1415
for (char c : s.toCharArray()) {
15-
if (c == second && !stack1.isEmpty() && stack1.peek() == first) {
16-
stack1.pop();
17-
maximumGain += big;
16+
if (c == second && !stack1.isEmpty() && stack1.peekLast() == first) {
17+
stack1.pollLast();
18+
max += big;
1819
} else {
19-
stack1.push(c);
20+
stack1.addLast(c);
2021
}
2122
}
22-
Stack<Character> stack2 = new Stack<>();
23+
Deque<Character> stack2 = new LinkedList<>();
2324
while (!stack1.isEmpty()) {
24-
char c = stack1.pop();
25-
if (c == second && !stack2.isEmpty() && stack2.peek() == first) {
26-
stack2.pop();
27-
maximumGain += small;
25+
char c = stack1.pollLast();
26+
if (!stack2.isEmpty() && c == second && stack2.peekLast() == first) {
27+
max += small;
28+
stack2.pollLast();
2829
} else {
29-
stack2.push(c);
30+
stack2.addLast(c);
3031
}
3132
}
32-
return maximumGain;
33+
return max;
3334
}
3435
}
3536

src/test/java/com/fishercoder/secondthousand/_1717Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.fishercoder.secondthousand;
22

33
import com.fishercoder.solutions.secondthousand._1717;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _1717Test {
1010
private static _1717.Solution1 solution1;
1111

12-
@BeforeClass
13-
public static void setup() {
12+
@BeforeEach
13+
public void setup() {
1414
solution1 = new _1717.Solution1();
1515
}
1616

0 commit comments

Comments
 (0)