diff --git a/container-with-most-water/socow.py b/container-with-most-water/socow.py new file mode 100644 index 000000000..e3b7b7a79 --- /dev/null +++ b/container-with-most-water/socow.py @@ -0,0 +1,34 @@ +""" +πŸ“š 11. Container With Most Water + +πŸ“Œ 문제 μš”μ•½ +- 높이 배열이 μ£Όμ–΄μ‘Œμ„ λ•Œ, 두 λ§‰λŒ€ 사이에 담을 수 μžˆλŠ” 물의 μ΅œλŒ€ 넓이 κ΅¬ν•˜κΈ° +- 넓이 = min(두 높이) Γ— 거리 + +🎯 핡심 μ•Œκ³ λ¦¬μ¦˜ +- νŒ¨ν„΄: 투 포인터 (Two Pointer) +- μ‹œκ°„λ³΅μž‘λ„: O(n) +- κ³΅κ°„λ³΅μž‘λ„: O(1) + +πŸ’‘ 핡심 아이디어 +1. μ–‘ λμ—μ„œ μ‹œμž‘ (left = 0, right = n-1) +2. ν˜„μž¬ 넓이 계산 ν›„ μ΅œλŒ“κ°’ κ°±μ‹  +3. 더 μž‘μ€ 높이 μͺ½μ„ 이동 β†’ 더 큰 높이λ₯Ό 찾을 κ°€λŠ₯μ„±! +""" + +class Solution: + def maxArea(self, height: list[int]) -> int: + left, right = 0, len(height) - 1 + max_area = 0 + + while left < right: + h = min(height[left], height[right]) + w = right - left + max_area = max(max_area, h * w) + + if height[left] < height[right]: + left += 1 + else: + right -= 1 + + return max_area diff --git a/valid-parentheses/socow.py b/valid-parentheses/socow.py new file mode 100644 index 000000000..47b9661cd --- /dev/null +++ b/valid-parentheses/socow.py @@ -0,0 +1,18 @@ +class Solution: + def isValid(self, s: str) -> bool: + + if len(s) % 2 ==1: + return False + + pair = {")":"(","}":"{","]":"["} + all = set(pair.values()) + stack = [] + + for x in s: + if x in all: + stack.append(x) + else: + if not stack or stack[-1] !=pair[x]: + return False + stack.pop() + return not stack