We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a9844ee commit afd9429Copy full SHA for afd9429
scripts/algorithms/M/Minimum Add to Make Parentheses Valid/Minimum Add to Make Parentheses Valid.py
@@ -1,17 +1,14 @@
1
-# Runtime: 53 ms (Top 37.79%) | Memory: 13.8 MB (Top 96.01%)
+// Runtime: 29 ms (Top 94.21%) | Memory: 16.40 MB (Top 63.56%)
2
+
3
class Solution:
4
def minAddToMakeValid(self, s: str) -> int:
- stack = []
5
-
6
- for parenthese in s:
7
- if parenthese == "(":
8
- stack.append("(")
9
+ l, r = list(), list()
+ for i in s:
+ if i == "(":
+ l.append(i)
10
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)
+ if l:
+ l.pop()
+ else:
+ r.append(i)
+ return len(l) + len(r)
0 commit comments