From 7811238773349db03d06c1187c78196074f24baf Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 17 Jan 2025 19:24:27 +0900 Subject: [PATCH 1/6] container-with-most-water solution --- container-with-most-water/sungjinwi.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 container-with-most-water/sungjinwi.py diff --git a/container-with-most-water/sungjinwi.py b/container-with-most-water/sungjinwi.py new file mode 100644 index 000000000..0cf1a16f6 --- /dev/null +++ b/container-with-most-water/sungjinwi.py @@ -0,0 +1,13 @@ +class Solution: + def maxArea(self, height: List[int]) -> int: + l = 0 + r = len(height) - 1 + max_area = 0 + while (l != r) : + cur_area = (r - l) * min(height[l], height[r]) + max_area = max(cur_area, max_area) + if (height[l] >= height[r]) : + r -= 1 + else : + l += 1 + return max_area From 01a553baf2023fd01298ca83d5aaef04852a010b Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Fri, 17 Jan 2025 19:24:53 +0900 Subject: [PATCH 2/6] valid-parentheses solution --- valid-parentheses/sungjinwi.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 valid-parentheses/sungjinwi.py diff --git a/valid-parentheses/sungjinwi.py b/valid-parentheses/sungjinwi.py new file mode 100644 index 000000000..b13ffb2ac --- /dev/null +++ b/valid-parentheses/sungjinwi.py @@ -0,0 +1,17 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + pair = dict(zip('({[',')}]')) + for paren in s : + if paren in pair : + stack.append(paren) + elif not stack : + return False + elif pair[stack[-1]] == paren : + stack.pop() + else: + return False + if not stack : + return True + else : + return False \ No newline at end of file From 6c5814e024ce811d3e3033cc07c0e5030a2a4780 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Sat, 18 Jan 2025 15:15:37 +0900 Subject: [PATCH 3/6] #222 valid-parentheses calculate complexity --- valid-parentheses/sungjinwi.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/valid-parentheses/sungjinwi.py b/valid-parentheses/sungjinwi.py index b13ffb2ac..9fa6f5663 100644 --- a/valid-parentheses/sungjinwi.py +++ b/valid-parentheses/sungjinwi.py @@ -1,3 +1,18 @@ +""" + 풀이 : + stack구조를 이용해서 구현 + 괄호의 페어를 딕셔너리의 key, value를 이용해 매치시킨다 + 여는 괄호를 만나면 stack에 push, 닫는 괄호를 만나면 stack의 마지막 괄호의 pair일 때 pop, 아니면 return False + 문자열 전체를 순회했을 때 stack에 남아있으면 안 닫힌 괄호가 있으므로 return False + + s의 길이 N + + TC : O(N) + s에 대해 한번 for문 + SC : O(N) + stack의 최대 크기는 s의 길이와 비례 +""" + class Solution: def isValid(self, s: str) -> bool: stack = [] @@ -7,11 +22,9 @@ def isValid(self, s: str) -> bool: stack.append(paren) elif not stack : return False - elif pair[stack[-1]] == paren : - stack.pop() - else: + elif pair[stack.pop()] != paren : return False if not stack : return True else : - return False \ No newline at end of file + return False From c934be0900bcf76e78427193628cd629eb31b039 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Sat, 18 Jan 2025 15:27:56 +0900 Subject: [PATCH 4/6] #242 valid-parentheses calculate complexity --- container-with-most-water/sungjinwi.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/container-with-most-water/sungjinwi.py b/container-with-most-water/sungjinwi.py index 0cf1a16f6..e2414dc28 100644 --- a/container-with-most-water/sungjinwi.py +++ b/container-with-most-water/sungjinwi.py @@ -1,3 +1,16 @@ +""" + 풀이 : + left, right 커서를 양 끝에 놓고 조건에 따라 반대편으로 이동시키며 물 양 비교 + 둘 중 더 높은 높이를 이동시키면 무조건 물의 양이 줄어들기 때문에 더 낮거나 같은 커서를 이동시키며 업데이트 + 둘이 만나면 비교 종료 + + + len(height) : N + TC : O(N) + l, r의 이동이 전체 height 개수만큼 일어나므로 + SC : O(1) +""" + class Solution: def maxArea(self, height: List[int]) -> int: l = 0 From 16b7109e9c1183eb4696c73bd5e465bee712e276 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Sat, 18 Jan 2025 16:06:56 +0900 Subject: [PATCH 5/6] #272 longest-increasing-subsequence solution --- longest-increasing-subsequence/sungjinwi.py | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 longest-increasing-subsequence/sungjinwi.py diff --git a/longest-increasing-subsequence/sungjinwi.py b/longest-increasing-subsequence/sungjinwi.py new file mode 100644 index 000000000..f46d2a297 --- /dev/null +++ b/longest-increasing-subsequence/sungjinwi.py @@ -0,0 +1,25 @@ +""" + 풀이 : + 해당 인덱스의 숫자로 끝나는 LIS의 길이를 dp배열에 저장 + nums[i] 와 그 이후에 오는 nums[j]를 비교해 nums[j]가 크고 + i까지의 LIS 길이 + 1 > j까지의 LIS길이이면 j까지의 LIS길이 업데이트 + 업데이트 할 때마다 max길이와 비교해서 최대길이 업데이트 + + nums의 길이 N + TC : O(N^2) + for문의 개수를 살펴보면 N-1 + N-2 + ... + 1 = (N-1)N/2 이므로 N^2/2 -> O(N^2) + + SC : O(N) + dp의 길이는 nums의 길이에 비례하므로 +""" + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [1] * len(nums) + max_LIS = 1 + for i in range(len(nums) - 1) : + for j in range(i + 1, len(nums)) : + if nums[i] < nums[j] and dp[i] + 1 > dp[j] : + dp[j] = dp[i] + 1 + max_LIS = max(max_LIS, dp[j]) + return max_LIS From 19e2f54936c252dfbb81d4de0fee40633e33ef4d Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Sat, 18 Jan 2025 20:18:01 +0900 Subject: [PATCH 6/6] #282 spiral-matrix solution --- spiral-matrix/sungjinwi.py | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 spiral-matrix/sungjinwi.py diff --git a/spiral-matrix/sungjinwi.py b/spiral-matrix/sungjinwi.py new file mode 100644 index 000000000..0f3fc5ba7 --- /dev/null +++ b/spiral-matrix/sungjinwi.py @@ -0,0 +1,42 @@ +""" + 풀이 : + 오른쪽, 아래쪽으로 이동할 땐 각각 column과 row가 증가하므로 direction 1 + 왼쪽, 위쪽은 감소하므로 direction -1 + 로 설정하고 n_row 또는 n_col만큼 direction을 이동하면서 ans에 담아준다 + 각 for문이 끝날 때마다 n_row 또는 n_col을 감소시켜 주고 + 둘 중 하나가 0이 될 때까지 계속 진행 + + - col이 -1부터 시작해서 matrix[0][0]부터 append할 수 있도록 유의 + - n_cols, n_rows 둘 중 하나만 0이되도 끝남에 유의 + + TC : O(M * N) + matrix 전체를 한번씩 순회해야하므로 + + SC : O(1) + return할 리스트 외에는 추가적인 메모리 사용이 상수개(5개)이므로 +""" + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + ans = [] + n_rows = len(matrix) + n_cols = len(matrix[0]) + + row = 0 + col = -1 + direction = 1 + + while n_cols and n_rows : + for _ in range(n_cols): + col += direction + ans.append(matrix[row][col]) + n_rows -= 1 + + for _ in range(n_rows): + row += direction + ans.append(matrix[row][col]) + n_cols -= 1 + + direction *= -1 + + return ans