Longest Valid Parentheses LeetCode solution in Python
1# Runtime: 74 ms (Top 48.14%) | Memory: 14.8 MB (Top 24.60%)2class Solution:3def longestValidParentheses(self, s: str) -> int:45maxi = 06stack = [-1]78for i in range(len(s)):9if s[i] == "(":10stack.append(i)11else:12stack.pop()13if len(stack) == 0:14stack.append(i)15else:16maxi = max(maxi, i - stack[-1])1718return maxi