From 89b1809a46fa911801b86462ccd3c20a7831cd13 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Mon, 26 Aug 2024 22:33:38 +0900 Subject: [PATCH 1/7] Solve two-sum with python --- two-sum/bemelon.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 two-sum/bemelon.py diff --git a/two-sum/bemelon.py b/two-sum/bemelon.py new file mode 100644 index 000000000..4faac7446 --- /dev/null +++ b/two-sum/bemelon.py @@ -0,0 +1,13 @@ +class Solution: + # Space complexity: O(n) + # Time complexity: O(n) + def twoSum(self, nums: list[int], target: int) -> list[int]: + num_index = {} + for curr, num in enumerate(nums): + rest = target - num + if rest in num_index: + return [num_index[rest], curr] + else: + num_index[num] = curr + return [0, 0] + From 9024ec52070f150002bdffe63f2b2aab493fb659 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Mon, 26 Aug 2024 22:45:23 +0900 Subject: [PATCH 2/7] Solve climbing-stairs with python --- climbing-stairs/bemelon.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 climbing-stairs/bemelon.py diff --git a/climbing-stairs/bemelon.py b/climbing-stairs/bemelon.py new file mode 100644 index 000000000..426b960bc --- /dev/null +++ b/climbing-stairs/bemelon.py @@ -0,0 +1,17 @@ +class Solution: + # Space complexity: O(1) + # Tiem complexity: O(n) + def climbStairs(self, n: int) -> int: + # dp[0] is n - 2 + # dp[1] is n - 1 + dp = [1, 2] + + if n <= 2: + return dp[n - 1] + + for i in range(3, n + 1): + # dp[n] = dp[n - 1] + dp[n - 2] + # = dp[1] + dp[0] + dp[(i - 1) % 2] = sum(dp) + + return dp[(n - 1) % 2] From d4361851c5d82a57f8df7f78d0a7f94d742706e8 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Mon, 26 Aug 2024 23:34:36 +0900 Subject: [PATCH 3/7] Solve product-of-array-except-self with python --- product-of-array-except-self/bemelon.py | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 product-of-array-except-self/bemelon.py diff --git a/product-of-array-except-self/bemelon.py b/product-of-array-except-self/bemelon.py new file mode 100644 index 000000000..67d693427 --- /dev/null +++ b/product-of-array-except-self/bemelon.py @@ -0,0 +1,46 @@ +class Solution: + # Space complexity: O(n) + # Time complexity: O(n) + def _v1(self, nums: list[int]) -> list[int]: + prefix = [1] + for num in nums[:-1]: + prefix.append(prefix[-1] * num) + + reverse_nums = nums[::-1] + postfix = [1] + for num in reverse_nums[:-1]: + postfix.append(postfix[-1] * num) + postfix = postfix[::-1] + + return [prefix[i] * postfix[i] for i in range(len(nums))] + + # Space complexity: O(1) + # Time complexity: O(n) + def _v2(self, nums: list[int]) -> list[int]: + n = len(nums) + answer = [1] * n + + # 1. save prefix product to temp + temp = 1 + for i in range(1, n): + temp *= nums[i - 1] + answer[i] *= temp + + # 2. save postfix product to temp + temp = 1 + for i in range(n - 2, -1, -1): + temp *= nums[i + 1] + answer[i] *= temp + + return answer + + + def productExceptSelf(self, nums: List[int]) -> List[int]: + # index -> product + # 0 -> - [1, 2, 3] + # 1 -> [0] - [2, 3] + # 2 -> [0, 1] - [3] + # 3 -> [0, 1, 2] - + return self._v2(nums) + + From ca069329b88fb7a553df793e6bc81716a99c7440 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Tue, 27 Aug 2024 23:45:22 +0900 Subject: [PATCH 4/7] Solve combination-sum with python --- combination-sum/bemelon.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 combination-sum/bemelon.py diff --git a/combination-sum/bemelon.py b/combination-sum/bemelon.py new file mode 100644 index 000000000..6cc697379 --- /dev/null +++ b/combination-sum/bemelon.py @@ -0,0 +1,20 @@ +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + list_of_combination = [] + n = len(candidates) + + def backtracking(curr: int, curr_combination: List[int], curr_sum: int): + if curr_sum == target: # 목표값에 도달했을 경우 + list_of_combination.append(list(curr_combination)) + return + + if curr_sum > target: # 목표값을 초과한 경우 + return + + for i in range(curr, n): + curr_combination.append(candidates[i]) + backtracking(i, curr_combination, curr_sum + candidates[i]) + curr_combination.pop() # 백트래킹 과정에서 마지막 요소 제거 + + backtracking(0, [], 0) + return list_of_combination From 0160a7bf6e5f80ee3b5fefb416b44d70f1bb91dc Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Tue, 27 Aug 2024 23:59:30 +0900 Subject: [PATCH 5/7] add complexity analysis --- combination-sum/bemelon.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/combination-sum/bemelon.py b/combination-sum/bemelon.py index 6cc697379..3a93d9f63 100644 --- a/combination-sum/bemelon.py +++ b/combination-sum/bemelon.py @@ -1,4 +1,9 @@ class Solution: + # Space complexity: O(n) + # - n: len(candidates) + # - Stack Frame -> O(n) + # - list_of_combination -> O(n) ? + # Time complexity: O(n!) def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: list_of_combination = [] n = len(candidates) From c70a5046b486a42d561b28e749467d0ca29ed57f Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Tue, 27 Aug 2024 23:59:42 +0900 Subject: [PATCH 6/7] Solve coin-change with python --- coin-change/bemelon.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 coin-change/bemelon.py diff --git a/coin-change/bemelon.py b/coin-change/bemelon.py new file mode 100644 index 000000000..3926cd27f --- /dev/null +++ b/coin-change/bemelon.py @@ -0,0 +1,16 @@ +class Solution: + # Space complexity: O(n) + # Time complexity: O(n * m) + # - n: amount + # - m: len(coins) + def coinChange(self, coins: list[int], amount: int) -> int: + INIT_VALUE = 999999999 + dp = [INIT_VALUE] * (amount + 1) + dp[0] = 0 + + for x in range(1, amount + 1): + for coin in coins: + if x - coin >= 0: + dp[x] = min(dp[x], dp[x - coin] + 1) + + return dp[amount] if dp[amount] != INIT_VALUE else -1 From cdc0d2a49a77ece03e7dc4033bc510a97c0cee64 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Wed, 28 Aug 2024 00:01:10 +0900 Subject: [PATCH 7/7] style --- product-of-array-except-self/bemelon.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/product-of-array-except-self/bemelon.py b/product-of-array-except-self/bemelon.py index 67d693427..a914fbc3c 100644 --- a/product-of-array-except-self/bemelon.py +++ b/product-of-array-except-self/bemelon.py @@ -1,7 +1,7 @@ class Solution: # Space complexity: O(n) # Time complexity: O(n) - def _v1(self, nums: list[int]) -> list[int]: + def naive(self, nums: list[int]) -> list[int]: prefix = [1] for num in nums[:-1]: prefix.append(prefix[-1] * num) @@ -16,7 +16,7 @@ def _v1(self, nums: list[int]) -> list[int]: # Space complexity: O(1) # Time complexity: O(n) - def _v2(self, nums: list[int]) -> list[int]: + def with_constant_space(self, nums: list[int]) -> list[int]: n = len(nums) answer = [1] * n @@ -41,6 +41,6 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: # 1 -> [0] - [2, 3] # 2 -> [0, 1] - [3] # 3 -> [0, 1, 2] - - return self._v2(nums) + return self.with_constant_space(nums)