-
-
Notifications
You must be signed in to change notification settings - Fork 339
[jylee2033] WEEK 03 solutions #2448
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
Open
jylee2033
wants to merge
4
commits into
DaleStudy:main
Choose a base branch
from
jylee2033:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 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,22 @@ | ||
| class Solution: | ||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| output = [] | ||
|
|
||
| def backtrack(remain, comb, start): | ||
| if remain == 0: | ||
| output.append(list(comb)) | ||
| return | ||
|
|
||
| elif remain < 0: | ||
| return | ||
|
|
||
| for i in range(start, len(candidates)): | ||
| comb.append(candidates[i]) | ||
| backtrack(remain - candidates[i], comb, i) | ||
| comb.pop() | ||
|
|
||
| backtrack(target, [], 0) | ||
| return output | ||
|
|
||
| # Time Complexity : O(N^T/M) N - number of candidates, T - target, M - minimum value in candidates | ||
| # Space Complexity : O(T/M) - maximum depth of the recursion tree |
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,27 @@ | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| # 1. Find highest power of 2 range that covers n | ||
| # 2. Greedily subtract from largest power of 2 to smallest | ||
|
|
||
| # powers : 1 + 2 + 4 + 8 + 16 + ... | ||
| # total : 1 + 3 + 7 + 15 + 31 + ... | ||
| # index : 0 1 2 3 4 | ||
|
|
||
| i = 0 | ||
| total = 1 | ||
|
|
||
| while n > total: | ||
| i += 1 | ||
| total += 2 ** i | ||
|
|
||
| count = 0 | ||
|
|
||
| for j in range(i, -1, -1): | ||
| if n >= 2 ** j: | ||
| n -= 2 ** j | ||
| count += 1 | ||
|
|
||
| return count | ||
|
|
||
| # Time Complexity : O(log n) | ||
| # Space Complexity : O(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,14 @@ | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| # 1. Remove non-alphanumeric characters and convert to lowercase | ||
| # 2. Compare with reversed string | ||
|
|
||
| cleaned_s = "" | ||
| for ch in s: | ||
| if ch.isalnum(): | ||
| cleaned_s += ch.lower() | ||
|
|
||
| return cleaned_s == cleaned_s[::-1] | ||
|
Contributor
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. 이런 풀이도 가능하겠네요! 코드가 깔끔해지는 풀이라고 생각되네요 :) 다만 문자열의 반까지만 나눠서 풀이를 하는 것도 가능하겠습니다~! for i in range(len(cleaned_s // 2):
if cleaned_s[i] != cleaned_s [len(cleaned_s) -1 - i]:
return False # early return 처리! |
||
|
|
||
| # Time Complexity : O(n) | ||
| # Space Complexity : O(n) | ||
|
Contributor
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. 투포인터를 이용하면 공간복잡도도 O(1)까지 줄일 수 있습니다🙌 |
||
Oops, something went wrong.
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.
커버 가능한 2의 거듭제곱수를 먼저 구하고 역으로 계산하신 방법으로 푸신 거군요!
직관적으로 이해하기 쉬운 방법인 것 같습니다 👍🏼
10진수를 2진수로 변경하는 문제는 나누기 연산자와 모듈러 연산자로도 풀이가 가능해서
저는 그런 방식으로 풀어보았습니다!