From cda7e0a90588ebe25269cb4124165cef28aa4365 Mon Sep 17 00:00:00 2001 From: Yunyoung Chung Date: Mon, 15 Dec 2025 23:15:16 +0900 Subject: [PATCH 1/3] week 6 - valid parentheses --- valid-parentheses/liza0525.py | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 valid-parentheses/liza0525.py diff --git a/valid-parentheses/liza0525.py b/valid-parentheses/liza0525.py new file mode 100644 index 0000000000..10c0d030be --- /dev/null +++ b/valid-parentheses/liza0525.py @@ -0,0 +1,41 @@ +# 시간 복잡도: O(n) +# - s의 길이만큼 탐색하기 때문에 +# 공간 복잡도: O(n) +# - stack을 list로 사용하며 s[:-1]까지 모든 다른 왼쪽 괄호일 때 O(n)까지 늘어날 수 있음 + +class Solution: + # 왼쪽 괄호는 stack에 저장하고 오른쪽 괄호가 들어왔을 때, + # stack의 가장 마지막에 들어온 왼쪽 괄호와 쌍이 맞는지 확인하여 + # 모든 경우에 쌍이 맞으면 이 문자열이 valid하다고 할 수 있다. + # 만약 유효성에서 falsy한 경우에는 early return 해준다. + def isValid(self, s: str) -> bool: + stack = [] + + for ss in s: + if ss in ('(', '[', '{'): + # 왼쪽 괄호는 stack에 저장 + stack.append(ss) + else: + # 오른쪽 괄호는 stack 내부 요소를 확인하여 유효성 확인 + if not stack: + # stack이 없는 경우에는 쌍을 맞출 왼쪽 괄호가 없으므로 s가 invalid하다고 할 수 있음 + return False + + # stack의 마지막 요소와 현재 확인할 문자열의 쌍이 맞는지 확인 + last_ss = stack[-1] + if any([ + last_ss == '(' and ss == ')', + last_ss == '[' and ss == ']', + last_ss == '{' and ss == '}', + ]): + # 쌍이 맞다면 stack의 마지막 문자열은 pop + stack.pop() + else: + # 쌍이 안 맞는 경우 + return False + if stack: + # 모든 문자열을 돌았는데도, stack에 왼쪽 괄호가 남아 있는 경우 + return False + + # 위의 모든 invalid한 경우를 통과했다면 valid하다고 할 수 있다. + return True From 0e45f5dcab7e284b8ba66009b86ede2bd0dad84b Mon Sep 17 00:00:00 2001 From: Yunyoung Chung Date: Fri, 19 Dec 2025 14:56:34 +0900 Subject: [PATCH 2/3] week 6 - container with most water --- container-with-most-water/liza0525.py | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 container-with-most-water/liza0525.py diff --git a/container-with-most-water/liza0525.py b/container-with-most-water/liza0525.py new file mode 100644 index 0000000000..c81280a514 --- /dev/null +++ b/container-with-most-water/liza0525.py @@ -0,0 +1,31 @@ +# 시간 복잡도: O(n) +# - two point 알고리즘을 적용했기 때문에, height의 길이 n만큼의 시간복잡도 사용 +# 공간 복잡도: O(1) +# - 입력 리스트 외에 상수 개수의 변수만 사용 + +class Solution: + # x의 길이를 변화 시킬 때 높이를 고려하며 변화 시킨다. + # 이 때 start 또는 end를 옮기는 기준은 각 포인트에서의 높이 중 작은 것을 옮긴다. + # 왜냐하면, 해당 문제에서 높이를 결정하는 것은 높이가 작은 쪽이며, + # 높이가 높은 것을 옮겨봤자 가로 길이만 짧아져 오히려 넓이는 축소가 되기 때문이다. + def maxArea(self, height: List[int]) -> int: + start = 0 + end = len(height) - 1 + result_area = (end - start) * min(height[start], height[end]) + + while start < end: + if height[start] < height[end]: + # start 쪽의 높이가 낮을 땐 start를 옮김 + start += 1 + else: + # end 쪽의 높이가 낮을 땐 end를 옮김(같을 때는 뭘 옮겨도 상관 없음) + end -= 1 + + # 옮긴 후의 넒이가, 지금까지 계산한 넓이보다 넓어진 것인지 확인하여 더 큰수를 + # result_area에 저장함 + result_area = max( + (end - start) * min(height[start], height[end]), + result_area + ) + + return result_area From c25e40a67531c224b3676b22a253c51060be99f2 Mon Sep 17 00:00:00 2001 From: Yunyoung Chung Date: Sat, 20 Dec 2025 12:08:19 +0900 Subject: [PATCH 3/3] =?UTF-8?q?week=206=20-=20valid=20parentheses=20(?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=ED=92=80=EC=9D=B4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-parentheses/liza0525.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/valid-parentheses/liza0525.py b/valid-parentheses/liza0525.py index 10c0d030be..2edcd060a4 100644 --- a/valid-parentheses/liza0525.py +++ b/valid-parentheses/liza0525.py @@ -39,3 +39,25 @@ def isValid(self, s: str) -> bool: # 위의 모든 invalid한 경우를 통과했다면 valid하다고 할 수 있다. return True + + # 리뷰 반영 풀이 + # 괄호의 쌍을 key-value로 하는 pairs 딕셔너리를 이용하여, if 조건을 더욱 간단하게 할 수 있음 + def isValid(self, s: str) -> bool: + stack = [] + pairs = {")": "(", "}": "{", "]": "["} + + for ss in s: + if ss in ('(', '[', '{'): + stack.append(ss) + else: + if not stack: + return False + + last_ss = stack[-1] + if last_ss == pairs[ss]: + stack.pop() + else: + return False + if stack: + return False + return True