Skip to content

Commit cfcdd05

Browse files
authored
Merge pull request #387 from BEMELON/main
[BEMELON] WEEK 3 Solution
2 parents 513430b + cdc0d2a commit cfcdd05

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

climbing-stairs/bemelon.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
# Space complexity: O(1)
3+
# Tiem complexity: O(n)
4+
def climbStairs(self, n: int) -> int:
5+
# dp[0] is n - 2
6+
# dp[1] is n - 1
7+
dp = [1, 2]
8+
9+
if n <= 2:
10+
return dp[n - 1]
11+
12+
for i in range(3, n + 1):
13+
# dp[n] = dp[n - 1] + dp[n - 2]
14+
# = dp[1] + dp[0]
15+
dp[(i - 1) % 2] = sum(dp)
16+
17+
return dp[(n - 1) % 2]

coin-change/bemelon.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
# Space complexity: O(n)
3+
# Time complexity: O(n * m)
4+
# - n: amount
5+
# - m: len(coins)
6+
def coinChange(self, coins: list[int], amount: int) -> int:
7+
INIT_VALUE = 999999999
8+
dp = [INIT_VALUE] * (amount + 1)
9+
dp[0] = 0
10+
11+
for x in range(1, amount + 1):
12+
for coin in coins:
13+
if x - coin >= 0:
14+
dp[x] = min(dp[x], dp[x - coin] + 1)
15+
16+
return dp[amount] if dp[amount] != INIT_VALUE else -1

combination-sum/bemelon.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
# Space complexity: O(n)
3+
# - n: len(candidates)
4+
# - Stack Frame -> O(n)
5+
# - list_of_combination -> O(n) ?
6+
# Time complexity: O(n!)
7+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
8+
list_of_combination = []
9+
n = len(candidates)
10+
11+
def backtracking(curr: int, curr_combination: List[int], curr_sum: int):
12+
if curr_sum == target: # 목표값에 도달했을 경우
13+
list_of_combination.append(list(curr_combination))
14+
return
15+
16+
if curr_sum > target: # 목표값을 초과한 경우
17+
return
18+
19+
for i in range(curr, n):
20+
curr_combination.append(candidates[i])
21+
backtracking(i, curr_combination, curr_sum + candidates[i])
22+
curr_combination.pop() # 백트래킹 과정에서 마지막 요소 제거
23+
24+
backtracking(0, [], 0)
25+
return list_of_combination
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution:
2+
# Space complexity: O(n)
3+
# Time complexity: O(n)
4+
def naive(self, nums: list[int]) -> list[int]:
5+
prefix = [1]
6+
for num in nums[:-1]:
7+
prefix.append(prefix[-1] * num)
8+
9+
reverse_nums = nums[::-1]
10+
postfix = [1]
11+
for num in reverse_nums[:-1]:
12+
postfix.append(postfix[-1] * num)
13+
postfix = postfix[::-1]
14+
15+
return [prefix[i] * postfix[i] for i in range(len(nums))]
16+
17+
# Space complexity: O(1)
18+
# Time complexity: O(n)
19+
def with_constant_space(self, nums: list[int]) -> list[int]:
20+
n = len(nums)
21+
answer = [1] * n
22+
23+
# 1. save prefix product to temp
24+
temp = 1
25+
for i in range(1, n):
26+
temp *= nums[i - 1]
27+
answer[i] *= temp
28+
29+
# 2. save postfix product to temp
30+
temp = 1
31+
for i in range(n - 2, -1, -1):
32+
temp *= nums[i + 1]
33+
answer[i] *= temp
34+
35+
return answer
36+
37+
38+
def productExceptSelf(self, nums: List[int]) -> List[int]:
39+
# index -> product
40+
# 0 -> - [1, 2, 3]
41+
# 1 -> [0] - [2, 3]
42+
# 2 -> [0, 1] - [3]
43+
# 3 -> [0, 1, 2] -
44+
return self.with_constant_space(nums)
45+
46+

two-sum/bemelon.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
# Space complexity: O(n)
3+
# Time complexity: O(n)
4+
def twoSum(self, nums: list[int], target: int) -> list[int]:
5+
num_index = {}
6+
for curr, num in enumerate(nums):
7+
rest = target - num
8+
if rest in num_index:
9+
return [num_index[rest], curr]
10+
else:
11+
num_index[num] = curr
12+
return [0, 0]
13+

0 commit comments

Comments
 (0)