일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Python Implementation
- Decorator
- Protocol
- 30. Substring with Concatenation of All Words
- concurrency
- 315. Count of Smaller Numbers After Self
- kaggle
- Substring with Concatenation of All Words
- 715. Range Module
- 파이썬
- shiba
- attribute
- Class
- 밴픽
- 프로그래머스
- Generator
- Python
- 43. Multiply Strings
- 109. Convert Sorted List to Binary Search Tree
- 운영체제
- 시바견
- LeetCode
- Python Code
- data science
- Convert Sorted List to Binary Search Tree
- t1
- 컴퓨터의 구조
- DWG
- iterator
- Regular Expression
Archives
- Today
- Total
Scribbling
32. Longest Valid Parentheses 본문
무지성 포칼포인트의 답...
class Solution:
def longestValidParentheses(self, s: str) -> int:
if not s:
return 0
mem = [0] * len(s)
stack = []
length = 0
for i in range(len(s)):
char = s[i]
if char == '(':
stack.append(char)
length = 0
else:
if not stack:
length = 0
else:
top = stack.pop()
if top == '(':
length += 2
if i - length >= 0 and s[i-length] == ')':
length += mem[i-length]
else:
length = 0
mem[i] = length
return max(mem)
괴수의 답...
class Solution:
def longestValidParentheses(self, s: str) -> int:
stack = []
dp = [0] * (len(s) + 1)
for i, c in enumerate(s):
if c == '(':
stack.append(i)
else:
if stack:
corresponding_idx = stack.pop()
dp[i+1] = dp[corresponding_idx] + i - corresponding_idx + 1
return max(dp)
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 48. Rotate Image (0) | 2021.09.05 |
---|---|
39. Combination Sum (0) | 2021.09.04 |
LeetCode: 29. Divide Two Integers (0) | 2021.09.02 |
LeetCode: 50. Pow(x, n) (0) | 2021.09.02 |
LeetCode: 30. Substring with Concatenation of All Words (0) | 2021.08.29 |