From c1d6281d7b1d3eadb25a9da673bb00bcf9820512 Mon Sep 17 00:00:00 2001 From: jinah92 Date: Sun, 29 Dec 2024 13:31:32 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20week4=20easy=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- merge-two-sorted-lists/jinah92.py | 18 ++++++++++++++++++ missing-number/jinah92.py | 12 ++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 merge-two-sorted-lists/jinah92.py create mode 100644 missing-number/jinah92.py diff --git a/merge-two-sorted-lists/jinah92.py b/merge-two-sorted-lists/jinah92.py new file mode 100644 index 000000000..d2fd2b872 --- /dev/null +++ b/merge-two-sorted-lists/jinah92.py @@ -0,0 +1,18 @@ +# O(m+n) times, O(1) spaces +class Solution: + def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: + dummy = ListNode(None) + node = dummy + + while list1 and list2: + if list1.val < list2.val: + node.next = list1 + list1 = list1.next + else: + node.next = list2 + list2 = list2.next + + node = node.next + + node.next = list1 or list2 + return dummy.next diff --git a/missing-number/jinah92.py b/missing-number/jinah92.py new file mode 100644 index 000000000..7c68dc983 --- /dev/null +++ b/missing-number/jinah92.py @@ -0,0 +1,12 @@ +# O(n) times, O(n) spaces +class Solution: + def missingNumber(self, nums: List[int]) -> int: + nums_keys = dict.fromkeys(nums,0) + last = 0 + + for i in range(len(nums)): + if not i in nums_keys: + return i + last += 1 + + return last From 6489b9be0c607d83e3a788d627bfce8cb4eca8f7 Mon Sep 17 00:00:00 2001 From: jinah92 Date: Sun, 29 Dec 2024 15:36:02 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20week4=20medium=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coin-change/jinah92.py | 10 ++++++++++ palindromic-substrings/jinah92.py | 18 ++++++++++++++++++ word-search/jinah92.py | 25 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 coin-change/jinah92.py create mode 100644 palindromic-substrings/jinah92.py create mode 100644 word-search/jinah92.py diff --git a/coin-change/jinah92.py b/coin-change/jinah92.py new file mode 100644 index 000000000..b528841ea --- /dev/null +++ b/coin-change/jinah92.py @@ -0,0 +1,10 @@ +# O(C*A) times, O(A) spaces +class Solution: + def coinChange(self, coins: List[int], amount: int) -> int: + dp = [0] + [amount + 1] * amount + + for coin in coins: + for i in range(coin, amount + 1): + dp[i] = min(dp[i], dp[i-coin]+1) + + return dp[amount] if dp[amount] < amount + 1 else -1 diff --git a/palindromic-substrings/jinah92.py b/palindromic-substrings/jinah92.py new file mode 100644 index 000000000..d5a4bf0a1 --- /dev/null +++ b/palindromic-substrings/jinah92.py @@ -0,0 +1,18 @@ +# O((LogN)^N) times, O(1) spaces +class Solution: + def countSubstrings(self, s: str) -> int: + sub_str_len = 1 + result = 0 + + while sub_str_len <= len(s): + start_idx = 0 + while start_idx + sub_str_len <= len(s): + sub_str = s[start_idx:start_idx+sub_str_len] + if sub_str == sub_str[::-1]: + result += 1 + start_idx += 1 + + sub_str_len += 1 + + + return result diff --git a/word-search/jinah92.py b/word-search/jinah92.py new file mode 100644 index 000000000..3f568eb95 --- /dev/null +++ b/word-search/jinah92.py @@ -0,0 +1,25 @@ +# O(M*N*4^W) times, O(M*N+W) spaces +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + rows, cols = len(board), len(board[0]) + visited = set() + + directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] + + def dfs(row, col, idx): + if idx == len(word): + return True + if not (0 <= row < rows and 0 <= col < cols): + return False + if board[row][col] != word[idx]: + return False + if (row, col) in visited: + return False + + visited.add((row, col)) + result = any(dfs(row+r, col+c, idx+1) for (r, c) in directions) + visited.remove((row, col)) + + return result + + return any(dfs(r, c, 0) for r in range(rows) for c in range(cols)) From c981bfbfa14e00cb56447df3ee6380de41e70f33 Mon Sep 17 00:00:00 2001 From: jinah92 Date: Wed, 1 Jan 2025 10:29:34 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20missing-number=20=EC=95=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=EC=A6=98=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - set 자료구조를 활용 - for문을 모두 순회한경우 리스트의 길이를 리턴 --- missing-number/jinah92.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/missing-number/jinah92.py b/missing-number/jinah92.py index 7c68dc983..357bb8070 100644 --- a/missing-number/jinah92.py +++ b/missing-number/jinah92.py @@ -1,12 +1,10 @@ # O(n) times, O(n) spaces class Solution: def missingNumber(self, nums: List[int]) -> int: - nums_keys = dict.fromkeys(nums,0) - last = 0 + nums_keys = set(nums) for i in range(len(nums)): if not i in nums_keys: return i - last += 1 - return last + return len(nums) From 35cfa418ad7b8409b3d7725212a0cbd3c370134d Mon Sep 17 00:00:00 2001 From: jinah92 Date: Wed, 1 Jan 2025 10:33:17 +0900 Subject: [PATCH 4/6] =?UTF-8?q?chore:=20=EC=8B=9C=EA=B0=84=EB=B3=B5?= =?UTF-8?q?=EC=9E=A1=EB=8F=84=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EC=84=A4?= =?UTF-8?q?=EB=AA=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- palindromic-substrings/jinah92.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/palindromic-substrings/jinah92.py b/palindromic-substrings/jinah92.py index d5a4bf0a1..cdd4da971 100644 --- a/palindromic-substrings/jinah92.py +++ b/palindromic-substrings/jinah92.py @@ -1,4 +1,7 @@ -# O((LogN)^N) times, O(1) spaces +# O(N^3) times, O(1) spaces +# 내부 while문의 관계가 외부 while문의 sub_str_len에 따라 반복횟수가 줄어드므로, 1+2+...N = N(N-1)/2 = O(N2) 시간 소요 +# 추라고 내부 while에서 sub_str_len에 따라 s가 인덱싱되므로 최대 O(N) 시간이 소요 +# 최종적으로 O(N^2 * N) = O(N^3)이 소요됨 class Solution: def countSubstrings(self, s: str) -> int: sub_str_len = 1 From 137592b248de9cf1154a03c4a6b8030d55e3aff8 Mon Sep 17 00:00:00 2001 From: jinah92 Date: Fri, 3 Jan 2025 11:08:50 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20word-search=20=EC=95=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=EC=A6=98=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81=20-?= =?UTF-8?q?=20=EA=B3=B5=EA=B0=84=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- word-search/jinah92.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/word-search/jinah92.py b/word-search/jinah92.py index 3f568eb95..df3015a93 100644 --- a/word-search/jinah92.py +++ b/word-search/jinah92.py @@ -1,9 +1,8 @@ -# O(M*N*4^W) times, O(M*N+W) spaces +# O(M*N*4^W) times, O(W) spaces class Solution: def exist(self, board: List[List[str]], word: str) -> bool: rows, cols = len(board), len(board[0]) - visited = set() - + directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] def dfs(row, col, idx): @@ -13,13 +12,13 @@ def dfs(row, col, idx): return False if board[row][col] != word[idx]: return False - if (row, col) in visited: - return False - visited.add((row, col)) + temp_val = board[row][col] + board[row][col] = "" + result = any(dfs(row+r, col+c, idx+1) for (r, c) in directions) - visited.remove((row, col)) + board[row][col] = temp_val return result return any(dfs(r, c, 0) for r in range(rows) for c in range(cols)) From e1d03cda2fca3863102f38caf5fbf3945d7f3def Mon Sep 17 00:00:00 2001 From: jinah92 Date: Fri, 3 Jan 2025 11:28:24 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20Palindromic=20Substrings=20?= =?UTF-8?q?=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98=20=EA=B0=9C=EC=84=A0=20-?= =?UTF-8?q?=20DP=20=ED=92=80=EC=9D=B4=EB=B2=95=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- palindromic-substrings/jinah92.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/palindromic-substrings/jinah92.py b/palindromic-substrings/jinah92.py index cdd4da971..05e36bad3 100644 --- a/palindromic-substrings/jinah92.py +++ b/palindromic-substrings/jinah92.py @@ -19,3 +19,21 @@ def countSubstrings(self, s: str) -> int: return result + +# DP 풀이 +# O(N^2) times, O(N^2) spaces +# start, end 지점을 순회하면서 이전 계산값을 재사용하여 회문을 파악 +class Solution2: + def countSubstrings(self, s: str) -> int: + dp = {} + + for end in range(len(s)): + for start in range(end, -1, -1): + if start == end: + dp[(start, end)] = True + elif start + 1 == end: + dp[(start, end)] = s[start] == s[end] + else: + dp[(start, end)] = s[start] == s[end] and dp[(start+1, end-1)] + + return list(dp.values()).count(True)