File tree 3 files changed +107
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
3 files changed +107
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
8
8
9
9
| # | Title | Solutions | Video | Difficulty | Tag
10
10
|-----|----------------|---------------|--------|-------------|-------------
11
+ | 1541| [ Minimum Insertions to Balance a Parentheses String] ( https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_1541.java ) | | Medium| String, Stack|
11
12
| 1539| [ Kth Missing Positive Number] ( https://leetcode.com/problems/kth-missing-positive-number/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_1539.java ) | | Easy| Array, HashTable|
12
13
| 1535| [ Find the Winner of an Array Game] ( https://leetcode.com/problems/find-the-winner-of-an-array-game/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_1535.java ) | [ :tv : ] ( https://youtu.be/v6On1TQfMTU ) | Medium| Array|
13
14
| 1534| [ Count Good Triplets] ( https://leetcode.com/problems/count-good-triplets/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_1534.java ) | | Easy| Array|
Original file line number Diff line number Diff line change
1
+ package com .fishercoder .solutions ;
2
+
3
+ import java .util .Stack ;
4
+
5
+ public class _1541 {
6
+ public static class Solution1 {
7
+ public int minInsertions (String s ) {
8
+ Stack <Character > stack = new Stack <>();
9
+ int insertionsNeeded = 0 ;
10
+ for (int i = 0 ; i < s .length (); i ++) {
11
+ char c = s .charAt (i );
12
+ if (c == '(' ) {
13
+ if (stack .isEmpty ()) {
14
+ stack .add (c );
15
+ } else {
16
+ if (stack .peek () == ')' ) {
17
+ //in this case, we need to add one more ')' to get two consecutive right paren, then we could pop the one ')' and one '(' off the stack
18
+ insertionsNeeded ++;
19
+ stack .pop ();
20
+ stack .pop ();
21
+ stack .add (c );
22
+ } else {
23
+ stack .add (c );
24
+ }
25
+ }
26
+ } else if (c == ')' ) {
27
+ if (stack .isEmpty ()) {
28
+ //in this case, we need to add one '(' before we add this ')' onto this stack
29
+ insertionsNeeded ++;
30
+ stack .add ('(' );
31
+ stack .add (c );
32
+ } else {
33
+ if (stack .peek () == ')' ) {
34
+ //in this case, we could pop the one ')' and one '(' off the stack
35
+ stack .pop ();
36
+ stack .pop ();
37
+ } else {
38
+ stack .add (c );
39
+ }
40
+ }
41
+ }
42
+ }
43
+ if (stack .isEmpty ()) {
44
+ return insertionsNeeded ;
45
+ } else {
46
+ while (!stack .isEmpty ()) {
47
+ char pop = stack .pop ();
48
+ if (pop == '(' ) {
49
+ insertionsNeeded += 2 ;
50
+ } else {
51
+ insertionsNeeded ++;
52
+ stack .pop ();
53
+ }
54
+ }
55
+ return insertionsNeeded ;
56
+ }
57
+ }
58
+ }
59
+ }
Original file line number Diff line number Diff line change
1
+ package com .fishercoder ;
2
+
3
+ import com .fishercoder .solutions ._1541 ;
4
+ import org .junit .BeforeClass ;
5
+ import org .junit .Test ;
6
+
7
+ import static org .junit .Assert .assertEquals ;
8
+
9
+ public class _1541Test {
10
+ private static _1541 .Solution1 solution1 ;
11
+
12
+ @ BeforeClass
13
+ public static void setup () {
14
+ solution1 = new _1541 .Solution1 ();
15
+ }
16
+
17
+ @ Test
18
+ public void test1 () {
19
+ assertEquals (1 , solution1 .minInsertions ("(()))" ));
20
+ }
21
+
22
+ @ Test
23
+ public void test2 () {
24
+ assertEquals (0 , solution1 .minInsertions ("())" ));
25
+ }
26
+
27
+ @ Test
28
+ public void test3 () {
29
+ assertEquals (3 , solution1 .minInsertions ("))())(" ));
30
+ }
31
+
32
+ @ Test
33
+ public void test4 () {
34
+ assertEquals (12 , solution1 .minInsertions ("((((((" ));
35
+ }
36
+
37
+ @ Test
38
+ public void test5 () {
39
+ assertEquals (5 , solution1 .minInsertions (")))))))" ));
40
+ }
41
+
42
+ @ Test
43
+ public void test6 () {
44
+ assertEquals (4 , solution1 .minInsertions ("(()))(()))()())))" ));
45
+ }
46
+
47
+ }
You can’t perform that action at this time.
0 commit comments