<gh_stars>1-10
## 32. 最长有效括号
class Solution:
    def longestValidParentheses(self, s: str) -> int:
        maxans = 0
        dp = [0] * len(s)
        for k in range(1, len(s)):
            if s[k] == ')':
                if s[k-1] == '(':
                    dp[k] = dp[k - 2] +  2 if k - 2 >= 0 else 2  
                elif k-dp[k-1]-1 >= 0 and s[k-dp[k-1]-1] == '(':
                    dp[k] = dp[k-1] + dp[k-dp[k-1]-2] + 2 if k-dp[k-1]-2 >= 0 else dp[k-1] + 2
                maxans = max(maxans, dp[k])
        return maxans
## 32. 最长有效括号
class Solution:
    def longestValidParentheses(self, s: str) -> int:
        maxans = 0
        dp = [0] * len(s)
        for k in range(1, len(s)):
            if s[k] == ')':
                if s[k-1] == '(':
                    dp[k] = dp[k - 2] +  2 if k - 2 >= 0 else 2  
                elif k-dp[k-1]-1 >= 0 and s[k-dp[k-1]-1] == '(':
                    dp[k] = dp[k-1] + dp[k-dp[k-1]-2] + 2 if k-dp[k-1]-2 >= 0 else dp[k-1] + 2
                maxans = max(maxans, dp[k])
        return maxans
## 32. 最长有效括号