Skip to content

Commit c2123be

Browse files
authored
Update 1541-minimum-insertions-to-balance-a-parentheses-string.js
1 parent 46a41a2 commit c2123be

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

1541-minimum-insertions-to-balance-a-parentheses-string.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,32 @@ const minInsertions = function(s) {
2121
}
2222
return right + res;
2323
};
24+
25+
// another
26+
27+
/**
28+
* @param {string} s
29+
* @return {number}
30+
*/
31+
const minInsertions = function(s) {
32+
let add = 0, req = 0 // number of parentheses added, number of closing parentheses required
33+
for(let ch of s) {
34+
if(ch === '(') {
35+
req += 2
36+
if(req % 2 === 1) {
37+
add++
38+
req--
39+
}
40+
} else {
41+
if(req === 0) {
42+
add++
43+
req++
44+
}else {
45+
req--
46+
}
47+
}
48+
}
49+
50+
return add + req
51+
};
52+

0 commit comments

Comments
 (0)