Skip to content

Commit 55c4df4

Browse files
committed
fix: 최적 알고리즘 사용하여 가독성 개선
1 parent 2ffbb37 commit 55c4df4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

valid-parentheses/gyeo-ri.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""
2+
[결과 요약]
3+
# 재시도횟수: 3회
4+
1. 조건에는 맞지만 메모리를 많이 사용 / 빈번한 False 리턴을 사용한 방법:: O(n)/O(n)
5+
2. 1의 로직의 가독성과 메모리 사용량 개선하기: O(n)/O(n)
6+
3. 실제로는 if/else문으로 풀어도 최적의 성능이 나오면서 가독성 측면에서도 유리: : O(n)/O(n)
7+
"""
8+
9+
110
class Solution:
211
def isValid(self, s: str) -> bool:
312
character_map = {"(": ")", "{": "}", "[": "]"}
@@ -16,6 +25,25 @@ def isValid(self, s: str) -> bool:
1625
return len(s_list) == 0
1726

1827

28+
"""
29+
# 실제로는 복잡한 로직 없이 if/else로 직접 비교하는 쪽이 성능과 코드 가독성 면에서 모두 유리
30+
class Solution:
31+
def isValid(self, s: str) -> bool:
32+
s_list = []
33+
34+
for c in s:
35+
if c == '(':
36+
s_list.append(')')
37+
elif c == '{':
38+
s_list.append('}')
39+
elif c == '[':
40+
s_list.append(']')
41+
else:
42+
if not s_list or s_list.pop() != c:
43+
return False
44+
"""
45+
46+
1947
if __name__ == "__main__":
2048
test_cases = [
2149
("{{{[[[(([{{}}]))]]]}}}", True),

0 commit comments

Comments
 (0)