파비의 매일매일 공부기록

Today's Challenge 본문

Problem Solving/LeetCode

Today's Challenge

fabichoi 2022. 5. 24. 23:45

https://leetcode.com/problems/longest-valid-parentheses/

 

Longest Valid Parentheses - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

스택을 이용한 LCS 구하는 문제.

class Solution:
    def longestValidParentheses(self, s: str) -> int:
        max_ans = 0
        st = [-1]
        for i in range(len(s)):
            if s[i] == '(':
                st.append(i)
            else:
                st.pop()
                if len(st) == 0:
                    st.append(i)
                else:
                    max_ans = max(max_ans, i-st[-1])
        return max_ans
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

Today's Challenge  (0) 2022.05.26
Today's Challenge  (0) 2022.05.25
Today's Challenge  (0) 2022.05.23
Today's Challenge  (0) 2022.05.22
Today's Challenge  (0) 2022.05.21
Comments