From c03226623188bd1ae0a29ac47eddb2581ac46c8c Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 28 Dec 2024 22:27:15 +0900 Subject: [PATCH 1/5] feat: two-sum --- two-sum/HodaeSsi.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 two-sum/HodaeSsi.py diff --git a/two-sum/HodaeSsi.py b/two-sum/HodaeSsi.py new file mode 100644 index 000000000..0142a77e8 --- /dev/null +++ b/two-sum/HodaeSsi.py @@ -0,0 +1,13 @@ +# 시간복잡도 : O(n) +# 공간복잡도 : O(n) +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + seen = {} # {num: idx, ...} + + for i, num in enumerate(nums): + if target - num in seen: + return [seen[target - num], i] + seen[num] = i + + return [] + From e48ec469530aeb7c68a0613b11901f93ad84525d Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 28 Dec 2024 22:27:15 +0900 Subject: [PATCH 2/5] feat: reverse-bits --- reverse-bits/HodaeSsi.py | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 reverse-bits/HodaeSsi.py diff --git a/reverse-bits/HodaeSsi.py b/reverse-bits/HodaeSsi.py new file mode 100644 index 000000000..6b67b91e2 --- /dev/null +++ b/reverse-bits/HodaeSsi.py @@ -0,0 +1,5 @@ +# 시간복잡도: O(1) (32bit) +class Solution: + def reverseBits(self, n: int) -> int: + return int(bin(n)[2:].zfill(32)[::-1], 2) + From b9e028deca9b491d443a9d9bbd271a6e13675516 Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 28 Dec 2024 22:27:15 +0900 Subject: [PATCH 3/5] feat: product-of-array-except-self --- product-of-array-except-self/HodaeSsi.py | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 product-of-array-except-self/HodaeSsi.py diff --git a/product-of-array-except-self/HodaeSsi.py b/product-of-array-except-self/HodaeSsi.py new file mode 100644 index 000000000..c77f6bc7e --- /dev/null +++ b/product-of-array-except-self/HodaeSsi.py @@ -0,0 +1,30 @@ +# 시간복잡도: O(n) +# 공간복잡도: O(n) +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + prefix = [1] * len(nums) + suffix = [1] * len(nums) + product = [1] * len(nums) + + for idx in range(len(nums)): + if idx == 0: + prefix[idx] = nums[idx] + else: + prefix[idx] = prefix[idx - 1] * nums[idx] + + for idx in range(len(nums) - 1, -1, -1): + if idx == len(nums) - 1: + suffix[idx] = nums[idx] + else: + suffix[idx] = suffix[idx + 1] * nums[idx] + + for idx in range(len(nums)): + if idx == 0: + product[idx] = suffix[idx + 1] + elif idx == len(nums) - 1: + product[idx] = prefix[idx - 1] + else: + product[idx] = prefix[idx - 1] * suffix[idx + 1] + + return product + From bd7c41309d24baf28e4027397f64bebead173684 Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 28 Dec 2024 22:27:15 +0900 Subject: [PATCH 4/5] feat: combination-sum --- combination-sum/HodaeSsi.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 combination-sum/HodaeSsi.py diff --git a/combination-sum/HodaeSsi.py b/combination-sum/HodaeSsi.py new file mode 100644 index 000000000..a5cb50ce5 --- /dev/null +++ b/combination-sum/HodaeSsi.py @@ -0,0 +1,16 @@ +# 시간복잡도 : O(n * m) (n: target, m: len(candidates)) +# 공간복잡도 : O(n * m) +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + dp = [[] for _ in range(target + 1)] + dp[0] = [[]] + + for candidate in candidates: + for num in range(candidate, target + 1): + for combination in dp[num - candidate]: + temp = combination.copy() + temp.extend([candidate]) + dp[num].append(temp) + + return dp[target] + From ffbcf9ab1001d4511fdad014834e60d4156c5954 Mon Sep 17 00:00:00 2001 From: "daeho.kim" Date: Sat, 28 Dec 2024 22:27:15 +0900 Subject: [PATCH 5/5] feat: maximum-subarray --- maximum-subarray/HodaeSsi.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 maximum-subarray/HodaeSsi.py diff --git a/maximum-subarray/HodaeSsi.py b/maximum-subarray/HodaeSsi.py new file mode 100644 index 000000000..e0b1dbf0e --- /dev/null +++ b/maximum-subarray/HodaeSsi.py @@ -0,0 +1,13 @@ +# 시간복잡도 : O(N) +# 공간복잡도 : O(1) +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + global_sum = nums[0] + local_sum = nums[0] + + for i in range(1, len(nums)): + local_sum = max(nums[i], local_sum + nums[i]) + global_sum = max(local_sum, global_sum) + + return global_sum +