From 1278bc044e46b0fb388ff6b45a4772af9703da80 Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 21 Dec 2024 22:05:33 +0900 Subject: [PATCH 1/6] feat: 242. Valid Anagram --- valid-anagram/HodaeSsi.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 valid-anagram/HodaeSsi.py diff --git a/valid-anagram/HodaeSsi.py b/valid-anagram/HodaeSsi.py new file mode 100644 index 000000000..73a89bd1e --- /dev/null +++ b/valid-anagram/HodaeSsi.py @@ -0,0 +1,19 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + sLetterDict = {} + tLetterDict = {} + + for letter in s: + sLetterDict[letter] = sLetterDict.get(letter, 0) + 1 + for letter in t: + tLetterDict[letter] = tLetterDict.get(letter, 0) + 1 + + if len(sLetterDict) != len(tLetterDict): + return False + + for sKey in sLetterDict.keys(): + if sKey not in tLetterDict or sLetterDict[sKey] != tLetterDict[sKey]: + return False + + return True + From 9776d05fd02f0e27bcc73575fecd800d4d7420ee Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 21 Dec 2024 22:05:33 +0900 Subject: [PATCH 2/6] feat: 70. Climbing Stairs --- climbing-stairs/HodaeSsi.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 climbing-stairs/HodaeSsi.py diff --git a/climbing-stairs/HodaeSsi.py b/climbing-stairs/HodaeSsi.py new file mode 100644 index 000000000..9569abed0 --- /dev/null +++ b/climbing-stairs/HodaeSsi.py @@ -0,0 +1,12 @@ +class Solution: + def climbStairs(self, n: int) -> int: + dp = [] + dp.append(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 c9c25a216dd44f2e17bcdb5af6858133387a4595 Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 21 Dec 2024 22:05:33 +0900 Subject: [PATCH 3/6] feat: 91. Decode Ways --- decode-ways/HodaeSsi.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 decode-ways/HodaeSsi.py diff --git a/decode-ways/HodaeSsi.py b/decode-ways/HodaeSsi.py new file mode 100644 index 000000000..f818f6fda --- /dev/null +++ b/decode-ways/HodaeSsi.py @@ -0,0 +1,28 @@ +class Solution: + def numDecodings(self, s: str) -> int: + dp = [] + if (s[0] == '0'): + return 0 + dp.append(1) + + for idx, _ in enumerate(s): + if idx == 0: + continue + if s[idx] == '0': + if s[idx-1] == '1' or s[idx-1] == '2': + if idx == 1: + dp.append(1) + else: + dp.append(dp[idx-2]) + else: + return 0 + elif s[idx-1] == '1' or (s[idx-1] == '2' and (s[idx] >= '1' and s[idx] <= '6')): + if idx == 1: + dp.append(2) + else: + dp.append(dp[idx-1] + dp[idx-2]) + else: + dp.append(dp[idx-1]) + + return dp[-1] + From fda6438537086e0ac1428c616330257d4d4a92ca Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 21 Dec 2024 22:05:33 +0900 Subject: [PATCH 4/6] feat: 105. Construct Binary Tree from Preorder and Inorder Traveral --- .../HodaeSsi.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/HodaeSsi.py diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/HodaeSsi.py b/construct-binary-tree-from-preorder-and-inorder-traversal/HodaeSsi.py new file mode 100644 index 000000000..bd42d364a --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/HodaeSsi.py @@ -0,0 +1,12 @@ +class Solution: + def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + if inorder == []: + return None + + mid = preorder.pop(0) + midIdx = inorder.index(mid) + left = self.buildTree(preorder, inorder[:midIdx]) + right = self.buildTree(preorder, inorder[midIdx + 1:]) + + return TreeNode(mid, left, right) + From 2d1ab08c48cf58f1d890d001e807090b87670159 Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 21 Dec 2024 22:05:33 +0900 Subject: [PATCH 5/6] feat: 15. 3Sum - Time Limit --- 3sum/HodaeSsi.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 3sum/HodaeSsi.py diff --git a/3sum/HodaeSsi.py b/3sum/HodaeSsi.py new file mode 100644 index 000000000..5bc0015c6 --- /dev/null +++ b/3sum/HodaeSsi.py @@ -0,0 +1,22 @@ +# 시간복잡도 O(n^2), 공간복잡도 O(n^2) +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + # key: 값, value: list((i, j)) + dic = {} + answer = set() + + # 이중 for문으로 모든 경우의 수를 구합니다. + for i in range(len(nums)): + for j in range(i+1, len(nums)): + if nums[i] + nums[j] in dic: + dic[nums[i] + nums[j]].append((i, j)) + else: + dic[nums[i] + nums[j]] = [(i, j)] + + for k in range(len(nums)): + for i, j in dic.get(-nums[k], []): + if i != k and j != k: + answer.add(tuple(sorted([nums[i], nums[j], nums[k]]))) + + return list(answer) + From 105a6b8620e8d5de9cf85f5a2aa4b4b407df8c6a Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 21 Dec 2024 22:05:33 +0900 Subject: [PATCH 6/6] feat: 15. 3Sum --- 3sum/HodaeSsi.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/3sum/HodaeSsi.py b/3sum/HodaeSsi.py index 5bc0015c6..6b8d4a9ad 100644 --- a/3sum/HodaeSsi.py +++ b/3sum/HodaeSsi.py @@ -1,22 +1,21 @@ -# 시간복잡도 O(n^2), 공간복잡도 O(n^2) class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: - # key: 값, value: list((i, j)) - dic = {} - answer = set() + answerSet = set() + nums.sort() - # 이중 for문으로 모든 경우의 수를 구합니다. - for i in range(len(nums)): - for j in range(i+1, len(nums)): - if nums[i] + nums[j] in dic: - dic[nums[i] + nums[j]].append((i, j)) + for i in range(len(nums) - 2): + leftIdx = i + 1 + rightIdx = len(nums) - 1 + while leftIdx < rightIdx: + sum = nums[i] + nums[leftIdx] + nums[rightIdx] + if sum < 0: + leftIdx += 1 + elif sum > 0: + rightIdx -= 1 else: - dic[nums[i] + nums[j]] = [(i, j)] + answerSet.add((nums[i], nums[leftIdx], nums[rightIdx])) + leftIdx = leftIdx + 1 + rightIdx = rightIdx - 1 - for k in range(len(nums)): - for i, j in dic.get(-nums[k], []): - if i != k and j != k: - answer.add(tuple(sorted([nums[i], nums[j], nums[k]]))) - - return list(answer) - + return list(answerSet) +