-
Notifications
You must be signed in to change notification settings - Fork 126
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
[kayden] Week 03 Solutions #393
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
abce85a
Two Sum Solution
39e61b9
Climbing Stairs Solution
870ef38
Add comments to the two sum solution
43ba99c
Product of Array Except Self Solution
819606d
Combination Sum Solution
c88e490
fix week02 solution
a157c44
fix error
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 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,12 @@ | ||
# 시간복잡도: O(N) | ||
# 공간복잡도: O(N) | ||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
dp = [0] * (n + 1) | ||
dp[0] = 1 | ||
dp[1] = 1 | ||
|
||
for i in range(2, n + 1): | ||
dp[i] = dp[i - 1] + dp[i - 2] | ||
|
||
return dp[n] |
This file contains 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,18 @@ | ||
# 시간복잡도: O(N) | ||
# 공간복잡도: out 제외시 O(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오해가 없도록 |
||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
n = len(nums) | ||
out = [1] * n | ||
|
||
prod = 1 | ||
for i in range(n - 1): | ||
prod *= nums[i] | ||
out[i + 1] *= prod | ||
|
||
prod = 1 | ||
for i in range(n - 1, 0, -1): | ||
prod *= nums[i] | ||
out[i - 1] *= prod | ||
|
||
return out |
This file contains 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,21 @@ | ||
# 시간복잡도: O(N) | ||
# 공간복잡도: O(N) | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
check = {} | ||
|
||
for idx, num in enumerate(nums): | ||
check[num] = idx | ||
|
||
for idx, num in enumerate(nums): | ||
# 동일한 숫자 두 개가 합쳐져서 목표값이 되는 경우 | ||
if num * 2 == target: | ||
# 그리고 그 숫자가 리스트에 두 개 이상 존재할 경우 | ||
if check[num] != idx: | ||
return [idx, check[num]] | ||
continue | ||
|
||
if target - num in check: | ||
return [check[num], check[target - num]] | ||
|
||
return [] |
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.
아 dp 를 사용하셨군요! 공간복잡도를
O(1)
로 줄일 수도 있는데 시간 되실 때 시도해보시면 좋을 것 같습니다!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.
답변 감사합니다! 다시 보니, 변수 두개로도 가능하겠군요!