-
-
Notifications
You must be signed in to change notification settings - Fork 248
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
89b1809
Solve two-sum with python
BEMELON 9024ec5
Solve climbing-stairs with python
BEMELON d436185
Solve product-of-array-except-self with python
BEMELON ca06932
Solve combination-sum with python
BEMELON 0160a7b
add complexity analysis
BEMELON c70a504
Solve coin-change with python
BEMELON cdc0d2a
style
BEMELON File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
return dp[(n - 1) % 2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오, 창의적이십니다!