728x90
1. The time complexity is Θ(n²) in the worst case.
class Solution(object):
def minAddToMakeValid(self, S):
"""
:type S: str
:rtype: int
"""
while "()" in S:
S = S.replace("()", "")
return len(S)
2. Easy to understand
class Solution(object):
def minAddToMakeValid(self, S):
"""
:type S: str
:rtype: int
"""
stack = []
count = 0
for i in S:
if i == '(':
stack.append(i)
elif stack:
stack.pop()
else:
count += 1
return (len(stack) + count)
728x90
'Leetcode' 카테고리의 다른 글
1249. Minimum Remove to Make Valid Parentheses (0) | 2020.08.04 |
---|---|
739. Daily Temperatures (0) | 2020.08.03 |
20. Valid Parentheses (0) | 2020.07.21 |
844. Backspace String Compare (0) | 2020.07.21 |
682. Baseball Game (0) | 2020.07.19 |
댓글