-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq32_Longest_Valid_Parentheses.py
More file actions
50 lines (47 loc) · 1.26 KB
/
q32_Longest_Valid_Parentheses.py
File metadata and controls
50 lines (47 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution:
def longestValidParentheses(self, s):
"""
:type s: str
:rtype: int
"""
left = []
maxv = 0
v = [(-1, -2)]
for j, pt in enumerate(s):
if pt == '(':
left.append(j)
if pt == ')' and left:
l = left.pop()
r = j
(pl, pr) = v[-1]
if r > pr and l < pl:
v.pop()
if l == v[-1][1] + 1:
l=v[-1][0]
v.pop()
v.append((l, r))
else:
v.append((l, r))
maxv = max(maxv,v[-1][1]-v[-1][0]+1)
return maxv
def longestValidParentheses1(self, s):
"""
:type s: str
:rtype: int
"""
dp = [0]*len(s)
left = 0
ans = 0
for i in range(0, len(s)):
if s[i] == "(":
left += 1
elif left > 0:
left -= 1
dp[i] = dp[i-1] + 2
j = i - dp[i]
if j >= 0:
dp[i] += dp[j]
ans = max(ans, dp[i])
return ans
s = Solution()
print(s.longestValidParentheses1('()()()(())((()'))