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] 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 diff --git a/combination-sum/bemelon.py b/combination-sum/bemelon.py new file mode 100644 index 000000000..3a93d9f63 --- /dev/null +++ b/combination-sum/bemelon.py @@ -0,0 +1,25 @@ +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) + + 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 diff --git a/product-of-array-except-self/bemelon.py b/product-of-array-except-self/bemelon.py new file mode 100644 index 000000000..a914fbc3c --- /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 naive(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 with_constant_space(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.with_constant_space(nums) + + 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] +