Skip to content

[BEMELON] WEEK 3 Solution #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions climbing-stairs/bemelon.py
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오, 창의적이십니다!


return dp[(n - 1) % 2]
16 changes: 16 additions & 0 deletions coin-change/bemelon.py
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions combination-sum/bemelon.py
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions product-of-array-except-self/bemelon.py
Original file line number Diff line number Diff line change
@@ -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)


13 changes: 13 additions & 0 deletions two-sum/bemelon.py
Original file line number Diff line number Diff line change
@@ -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]