From 9fcc0bc06634342c0a32d9aa5a3ebaa2573df4be Mon Sep 17 00:00:00 2001 From: ChaedongIm Date: Sun, 15 Dec 2024 18:15:55 +0900 Subject: [PATCH 1/9] feat: [Week 02-1] solve valid-anagram --- valid-anagram/Chaedie.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 valid-anagram/Chaedie.py diff --git a/valid-anagram/Chaedie.py b/valid-anagram/Chaedie.py new file mode 100644 index 000000000..4d5eed1d7 --- /dev/null +++ b/valid-anagram/Chaedie.py @@ -0,0 +1,27 @@ +''' +count a frequency of the character with a hash map +s's character will be added and +t's character will be subtracted + +return True if count's min and max value is not a zero +else return False + +Time O(n) +Space O(n) +''' + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + count = defaultdict(int) + + if len(t) != len(s): + return False + + for i in range(len(s)): + count[s[i]] += 1 + count[t[i]] -= 1 + + if max(count.values()) == 0: + return True + return False + From f00520eee1c277522397672618e7298bcc050704 Mon Sep 17 00:00:00 2001 From: ChaedongIm Date: Sun, 15 Dec 2024 18:16:15 +0900 Subject: [PATCH 2/9] feat: [Week 02-2] solve climbing-stairs --- climbing-stairs/Chaedie.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 climbing-stairs/Chaedie.py diff --git a/climbing-stairs/Chaedie.py b/climbing-stairs/Chaedie.py new file mode 100644 index 000000000..ba6fede35 --- /dev/null +++ b/climbing-stairs/Chaedie.py @@ -0,0 +1,23 @@ +''' +solution: + # dp[1] = 1step = 1 + # dp[2] = (1step + 1step) + 2step = 2 + # dp[3] = (dp[3 - 1] + 1step) + dp[3 - 2] + 2step = 2 + 1 = 3 + # dp[4] = (dp[4 - 1] + 1step) + (dp[4 - 2] + 2tep) = 3 + 2 = 5 + + # dp[n] = (dp[n-1] + 1) + (dp[n-2] + 1) + +time O(n) +space O(n) + +''' + +class Solution: + def climbStairs(self, n: int) -> int: + dp = [0] + dp.append(1) + dp.append(2) + + for i in range(3, n+1): + dp.append(dp[i-1] + dp[i-2]) + return dp[n] From 6642c7bea3d3f1b9963dfe41d4e4e24ce0329a59 Mon Sep 17 00:00:00 2001 From: ChaedongIm Date: Tue, 17 Dec 2024 09:08:09 +0900 Subject: [PATCH 3/9] feat: [Week 02-3] solve 3 sum --- 3sum/Chaedie.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 3sum/Chaedie.py diff --git a/3sum/Chaedie.py b/3sum/Chaedie.py new file mode 100644 index 000000000..4e0e16cdd --- /dev/null +++ b/3sum/Chaedie.py @@ -0,0 +1,59 @@ +""" +첫번째 풀이 -> 달레의 코드 풀이 +1) sort와 two pointer를 활용한 풀이 +2) has_set 을 활용한 중복 제거 + +두번째 풀이 -> Neetcode 풀이 +1) sort와 two pointer를 활용한 풀이 +2) while loop 를 활용한 중복 제거 + +Time: O(n) = O(n) + O(n) +Space: O(n) +""" + + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + res = set() + n = len(nums) + + for i in range(n): + l, r = i + 1, n - 1 + while l < r: + summ = nums[i] + nums[l] + nums[r] + if summ < 0: + l += 1 + elif summ > 0: + r -= 1 + else: + res.add((nums[i], nums[l], nums[r])) + l += 1 + return list(res) + + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + res = [] + n = len(nums) + + for i in range(n): + l, r = i + 1, n - 1 + + if i > 0 and nums[i] == nums[i - 1]: + continue + + while l < r: + summ = nums[i] + nums[l] + nums[r] + if summ < 0: + l += 1 + elif summ > 0: + r -= 1 + else: + res.append([nums[i], nums[l], nums[r]]) + l += 1 + while nums[l] == nums[l - 1] and l < r: + l += 1 + + return res From 4ddb7c6a781de9750736225c42cd39b4b47fc1cf Mon Sep 17 00:00:00 2001 From: ChaedongIm Date: Tue, 17 Dec 2024 09:25:14 +0900 Subject: [PATCH 4/9] feat: [Week 02-4] solve construct-binary-tree-from-preorder-and-inorder-traversal --- .../Chaedie.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/Chaedie.py diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/Chaedie.py b/construct-binary-tree-from-preorder-and-inorder-traversal/Chaedie.py new file mode 100644 index 000000000..7432a4100 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/Chaedie.py @@ -0,0 +1,29 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right + +""" +1) preorder 의 경우 젤 첫번째 노드가 최상단 노드라는 보장이 있음 +2) inorder 의 경우 preorder 에서의 첫번째 node 를 기준으로 왼쪽이 left 노드, 오른찍이 right 노드라는 보장이 있음 +3) preorder 에서 left, right의 갯수는 inorder에서 찾은 root node 의 인덱스 를 활용할 수 있음 + +Time: O(n^2) +Space: O(n^2) +""" + + +class Solution: + def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + if not preorder or not inorder: + return None + + newNode = TreeNode() + newNode.val = preorder[0] + mid = inorder.index(preorder[0]) + newNode.left = self.buildTree(preorder[1 : mid + 1], inorder[:mid]) + newNode.right = self.buildTree(preorder[mid + 1 :], inorder[mid + 1 :]) + + return newNode From d12889a66d5c8f22d7dd1f5cb2df9f39656318bb Mon Sep 17 00:00:00 2001 From: ChaedongIm Date: Tue, 17 Dec 2024 09:47:17 +0900 Subject: [PATCH 5/9] feat: [Week 02-5] solve decode-ways --- decode-ways/Chaedie.py | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 decode-ways/Chaedie.py diff --git a/decode-ways/Chaedie.py b/decode-ways/Chaedie.py new file mode 100644 index 000000000..19d0afab4 --- /dev/null +++ b/decode-ways/Chaedie.py @@ -0,0 +1,57 @@ +""" +dp 문제는 아직 어색해서 직접 풀진 못했습니다. +""" + +""" +재귀와 dp hash_map 을 활용한 캐싱 전략 +Time: O(n) +Space: O(n) = O(n) + O(n) +""" + + +class Solution: + def numDecodings(self, s: str) -> int: + dp = {len(s): 1} + + def dfs(i): + if i in dp: + return dp[i] + if s[i] == "0": + return 0 + + # 한자리 숫자 + res = dfs(i + 1) + + # 두자리 숫자 + if i + 1 < len(s) and ( + s[i] == "1" or s[i] == "2" and s[i + 1] in "0123456" + ): + res += dfs(i + 2) + dp[i] = res + return res + + return dfs(0) + + +""" +iterative dp +Time: O(n) +Space: O(n) +""" + + +class Solution: + def numDecodings(self, s: str) -> int: + dp = {len(s): 1} + + for i in range(len(s) - 1, -1, -1): + if s[i] == "0": + dp[i] = 0 + else: + dp[i] = dp[i + 1] + + if i + 1 < len(s) and ( + s[i] == "1" or s[i] == "2" and s[i + 1] in "0123456" + ): + dp[i] += dp[i + 2] + return dp[0] From 72aaa3ae138c71be564a2b8771bbb0223b77377b Mon Sep 17 00:00:00 2001 From: ChaedongIm Date: Sun, 22 Dec 2024 11:40:49 +0900 Subject: [PATCH 6/9] =?UTF-8?q?fix:=20=EC=8B=9C=EA=B0=84=20=EB=B3=B5?= =?UTF-8?q?=EC=9E=A1=EB=8F=84=20=EA=B3=84=EC=82=B0=20=EC=8B=A4=EC=88=98=20?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3sum/Chaedie.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3sum/Chaedie.py b/3sum/Chaedie.py index 4e0e16cdd..e22880c8a 100644 --- a/3sum/Chaedie.py +++ b/3sum/Chaedie.py @@ -7,7 +7,7 @@ 1) sort와 two pointer를 활용한 풀이 2) while loop 를 활용한 중복 제거 -Time: O(n) = O(n) + O(n) +Time: O(n^2) = O(n) * O(n) Space: O(n) """ From 6ba9c38b47fd71a355e15190d06c023ee9c66020 Mon Sep 17 00:00:00 2001 From: ChaedongIm Date: Sun, 22 Dec 2024 11:49:19 +0900 Subject: [PATCH 7/9] =?UTF-8?q?feat:=20climbing-stairs=20=EA=B3=B5?= =?UTF-8?q?=EA=B0=84=20=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EC=B5=9C=EC=A0=81?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- climbing-stairs/Chaedie.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/climbing-stairs/Chaedie.py b/climbing-stairs/Chaedie.py index ba6fede35..c49141771 100644 --- a/climbing-stairs/Chaedie.py +++ b/climbing-stairs/Chaedie.py @@ -21,3 +21,19 @@ def climbStairs(self, n: int) -> int: for i in range(3, n+1): dp.append(dp[i-1] + dp[i-2]) return dp[n] + +''' +solution2: + 위 솔루션에서 공간 복잡도 최적화 + +time O(n) +space O(1) +''' +class Solution: + def climbStairs(self, n: int) -> int: + pt1, pt2 = 1,1 + for i in range(2, n+1): + temp = pt2 + pt2 = pt1 + pt2 + pt1 = temp + return pt2 \ No newline at end of file From 717a13242b2029d02b6c17cc6a61e75b23801209 Mon Sep 17 00:00:00 2001 From: ChaedongIm Date: Sun, 22 Dec 2024 11:49:38 +0900 Subject: [PATCH 8/9] =?UTF-8?q?fix:=20eol=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- climbing-stairs/Chaedie.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/climbing-stairs/Chaedie.py b/climbing-stairs/Chaedie.py index c49141771..adf087064 100644 --- a/climbing-stairs/Chaedie.py +++ b/climbing-stairs/Chaedie.py @@ -36,4 +36,4 @@ def climbStairs(self, n: int) -> int: temp = pt2 pt2 = pt1 + pt2 pt1 = temp - return pt2 \ No newline at end of file + return pt2 From 97eb6f9759f08484a9e0b52a458dc61c3e24cdad Mon Sep 17 00:00:00 2001 From: ChaedongIm Date: Sun, 22 Dec 2024 12:35:55 +0900 Subject: [PATCH 9/9] =?UTF-8?q?feat:=20add=20Solution=20for=20=EC=B5=9C?= =?UTF-8?q?=EC=A0=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Chaedie.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/Chaedie.py b/construct-binary-tree-from-preorder-and-inorder-traversal/Chaedie.py index 7432a4100..8587dd26a 100644 --- a/construct-binary-tree-from-preorder-and-inorder-traversal/Chaedie.py +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/Chaedie.py @@ -27,3 +27,31 @@ def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNod newNode.right = self.buildTree(preorder[mid + 1 :], inorder[mid + 1 :]) return newNode + +''' +알고달레 참고해서 최적화 진행했습니다. +1) 시간복잡도 최적화: dictionary 를 활용해 mid 를 찾는 index 메서드 제거 +2) 공간복잡도 최적화: preorder, inorder 배열을 넘기는것이 아닌 index 만 넘김 + +Time: O(n) +Space: O(n) + +''' + +class Solution: + def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + indices = { val: idx for idx, val in enumerate(inorder) } + pre_iter = iter(preorder) + + def dfs(start, end): + if start > end: + return None + + val = next(pre_iter) + mid = indices[val] + + left = dfs(start, mid - 1) + right = dfs(mid + 1, end) + return TreeNode(val, left, right) + + return dfs(0, len(inorder) - 1)