-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sejineer] Week 03 solutions #1291
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
5 commits
Select commit
Hold shift + click to select a range
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,15 @@ | ||
""" | ||
시간 복잡도: O(target * n) | ||
공간 복잡도: O(n)? | ||
개인적으로 어려웠던 문제라서 정답을 봤습니다. | ||
추후에 다시 복습할 예정입니다. | ||
""" | ||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
dp = [[] for _ in range(target + 1)] | ||
dp[0] = [[]] | ||
for candidate in candidates: | ||
for num in range(candidate, target + 1): | ||
for combination in dp[num - candidate]: | ||
dp[num].append(combination + [candidate]) | ||
return dp[target] |
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 @@ | ||
""" | ||
시간 복잡도: O(N) | ||
공간 복잡도: O(N) | ||
""" | ||
class Solution: | ||
def numDecodings(self, s: str) -> int: | ||
dp = [0] * len(s) + [1] | ||
|
||
for i in reversed(range(len(s))): | ||
if s[i] == "0": | ||
dp[i] = 0 | ||
elif i + 1 < len(s) and int(s[i : i + 2]) < 27: | ||
dp[i] = dp[i + 1] + dp[i + 2] | ||
else: | ||
dp[i] = dp[i + 1] | ||
|
||
return dp[0] |
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 @@ | ||
""" | ||
시간 복잡도 O(N) | ||
공간 복잡도 O(N) | ||
""" | ||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
dp = [0] * len(nums) | ||
dp[0] = nums[0] | ||
|
||
for i in range(1, len(nums)): | ||
dp[i] = max(nums[i], dp[i - 1] + nums[i]) | ||
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. 반복문과 dp 배열의 의미가 깔끔하게 드러나는 핵심 부분이라 가독성이 좋네요! 😊 |
||
|
||
return max(dp) |
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 @@ | ||
""" | ||
시간 복잡도: O(logN) | ||
공간 복잡도: O(1) | ||
""" | ||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
result = 1 | ||
while n // 2 != 0: | ||
a = n // 2 | ||
b = n % 2 | ||
result += b | ||
n = a | ||
return result |
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,8 @@ | ||
""" | ||
시간 복잡도: O(N) | ||
공간 복잡도: O(N) | ||
""" | ||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
filterd_s = [ch for ch in s.lower() if ch.isalnum()] | ||
return filterd_s == filterd_s[::-1] |
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)으로 bottom-up 방식 구현하신 거 잘 정리하셨네요.
특히
dp[num].append(combination + [candidate])
로 조합이 누적되는 흐름이 명확하게 보여서 좋았어요! 👏나중에 복습하실 때도 이 로직이 잘 기억날 것 같아요.