upd
This commit is contained in:
15
algorithms/labs/lab6/leetcode/2/solution.py
Normal file
15
algorithms/labs/lab6/leetcode/2/solution.py
Normal file
@@ -0,0 +1,15 @@
|
||||
class Solution:
|
||||
def longestValidParentheses(self, s: str) -> int:
|
||||
n = len(s)
|
||||
dp = [0] * n
|
||||
best = 0
|
||||
for i in range(1, n):
|
||||
if s[i] == ")":
|
||||
if s[i - 1] == "(":
|
||||
dp[i] = (dp[i - 2] if i >= 2 else 0) + 2
|
||||
elif i - dp[i - 1] - 1 >= 0 and s[i - dp[i - 1] - 1] == "(":
|
||||
dp[i] = dp[i - 1] + 2
|
||||
if i - dp[i - 1] - 2 >= 0:
|
||||
dp[i] += dp[i - dp[i - 1] - 2]
|
||||
best = max(best, dp[i])
|
||||
return best
|
||||
Reference in New Issue
Block a user