File tree 2 files changed +22
-21
lines changed
main/java/com/fishercoder/solutions/secondthousand
test/java/com/fishercoder/secondthousand 2 files changed +22
-21
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions .secondthousand ;
2
2
3
- import java .util .Stack ;
3
+ import java .util .Deque ;
4
+ import java .util .LinkedList ;
4
5
5
6
public class _1717 {
6
7
public static class Solution1 {
7
8
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 );
10
10
int small = big == x ? y : x ;
11
- char first = x == big ? 'a' : 'b' ;
11
+ char first = big == x ? 'a' : 'b' ;
12
12
char second = first == 'a' ? 'b' : 'a' ;
13
- int maximumGain = 0 ;
13
+ Deque <Character > stack1 = new LinkedList <>();
14
+ int max = 0 ;
14
15
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 ;
18
19
} else {
19
- stack1 .push (c );
20
+ stack1 .addLast (c );
20
21
}
21
22
}
22
- Stack <Character > stack2 = new Stack <>();
23
+ Deque <Character > stack2 = new LinkedList <>();
23
24
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 () ;
28
29
} else {
29
- stack2 .push (c );
30
+ stack2 .addLast (c );
30
31
}
31
32
}
32
- return maximumGain ;
33
+ return max ;
33
34
}
34
35
}
35
36
Original file line number Diff line number Diff line change 1
1
package com .fishercoder .secondthousand ;
2
2
3
3
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 ;
6
6
7
- import static org .junit .Assert .assertEquals ;
7
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
8
8
9
9
public class _1717Test {
10
10
private static _1717 .Solution1 solution1 ;
11
11
12
- @ BeforeClass
13
- public static void setup () {
12
+ @ BeforeEach
13
+ public void setup () {
14
14
solution1 = new _1717 .Solution1 ();
15
15
}
16
16
You can’t perform that action at this time.
0 commit comments