Skip to content

Commit afd9429

Browse files
committed
Runtime: 29 ms (Top 94.21%) | Memory: 16.40 MB (Top 63.56%)
1 parent a9844ee commit afd9429

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
# Runtime: 53 ms (Top 37.79%) | Memory: 13.8 MB (Top 96.01%)
1+
// Runtime: 29 ms (Top 94.21%) | Memory: 16.40 MB (Top 63.56%)
2+
23
class Solution:
34
def minAddToMakeValid(self, s: str) -> int:
4-
stack = []
5-
6-
for parenthese in s:
7-
if parenthese == "(":
8-
stack.append("(")
9-
5+
l, r = list(), list()
6+
for i in s:
7+
if i == "(":
8+
l.append(i)
109
else:
11-
if not stack or stack[-1] == ")":
12-
stack.append(")")
13-
14-
if stack and stack[-1] == "(":
15-
stack.pop()
16-
17-
return len(stack)
10+
if l:
11+
l.pop()
12+
else:
13+
r.append(i)
14+
return len(l) + len(r)

0 commit comments

Comments
 (0)