From 37aea7df7fdc2ab80407d835205df1086f667d59 Mon Sep 17 00:00:00 2001 From: vmop899 Date: Sun, 19 Jan 2025 01:14:17 +0900 Subject: [PATCH] FEAT : solved 3 problems --- container-with-most-water/aa601.py | 15 +++++++++++++++ longest-increasing-subsequence/aa601.py | 23 +++++++++++++++++++++++ valid-parentheses/aa601.py | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 container-with-most-water/aa601.py create mode 100644 longest-increasing-subsequence/aa601.py create mode 100644 valid-parentheses/aa601.py diff --git a/container-with-most-water/aa601.py b/container-with-most-water/aa601.py new file mode 100644 index 000000000..80d670582 --- /dev/null +++ b/container-with-most-water/aa601.py @@ -0,0 +1,15 @@ +class Solution: + def maxArea(self, height: List[int]) -> int: + right = len(height) - 1 + left = 0 + max_size = 0 + for line in range(len(height)): + if left >= right: + break + cur_size = (right - left) * min(height[left], height[right]) + if height[left] < height[right]: # 왼쪽이 작으면 left 이동 + left += 1 + else: + right -= 1 + max_size = max(max_size, cur_size) + return max_size diff --git a/longest-increasing-subsequence/aa601.py b/longest-increasing-subsequence/aa601.py new file mode 100644 index 000000000..b7ffe79e4 --- /dev/null +++ b/longest-increasing-subsequence/aa601.py @@ -0,0 +1,23 @@ +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [1 for _ in range(len(nums))] + prv = [-1 for _ in range(len(nums))] + for i in range(len(nums)): + for j in range(i + 1, len(nums)): + if nums[i] < nums[j]: + if dp[i] + 1 > dp[j]: + dp[j] = dp[i] + 1 + """ + prv [-1, -1, -1, 2, 2, 3, 5, 5] + dp [ 1, 1, 1, 2, 2, 3, 4, 4] + nums[10, 9, 2, 5, 3, 7, 101, 18] + nums[2] = 2 + nums[4] = 3 + nums[5] = 7 + + #dp[j] = max(dp[i] + 1, dp[j]) + # dp[i] + 1 <= dp[j]일 때 dp[j] 갱신X + # LIS길이가 더 적거나 같은 상황. => LIS배열이 업데이트X + """ + return max(dp) + \ No newline at end of file diff --git a/valid-parentheses/aa601.py b/valid-parentheses/aa601.py new file mode 100644 index 000000000..83aaf2c39 --- /dev/null +++ b/valid-parentheses/aa601.py @@ -0,0 +1,19 @@ +class Solution: + def isValid(self, s: str) -> bool: + # stack 활용, 짝이 맞으면 pop하기 + stack = [] + dic = dict(zip(")}]", "({[")) # close bracket : open bracket 형태로 사전 생성 + for char in s: + if char in "({[": # open bracket인 경우 stack에 추가 + stack.append(char) + elif char in ")}]" : # close bracket이면서 + if not stack or stack.pop() != dic[char]: + return False + + # len()함수 시간복잡도 먹음 O(n) + # => not stack 사용하면 개선됨 + if len(stack) == 0: # stack이 비어있다면 모든 짝이 맞아서 pop되었으므로 true 반환 + return True + else: + return False + \ No newline at end of file