-
-
Notifications
You must be signed in to change notification settings - Fork 304
[Seoya0512] WEEK03 Solutions #2104
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
3 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 @@ | ||
| ''' | ||
| Approach | ||
| - target값에서 candidate 값을 빼고 그 누적합을 사용해야한다는 흐름은 파악했지지만 구현이 어려웠습니다. | ||
| -그래서 알고달레를 참고해서 최대한 이해하고 혼자 작성해보려고 했습니다.... | ||
| ''' | ||
| 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] |
|
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. 깔끔합니다! 👍 |
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,20 @@ | ||
| ''' | ||
| Approach | ||
| - 십진법을 이진법으로 변환하는 방식과 누적합을 사용함 | ||
|
|
||
| Time Complexity: O(log n) | ||
| - while 문에서 숫자(num)을 계속해서 2로 나누는데 소요되는 시간 | ||
|
|
||
| Space Complexity: O(1) | ||
| - 상수 bits와 nums를 저장하는 공간 | ||
| ''' | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| bits = 0 | ||
| # 주어진 숫자를 2로 나눈 나머지 값이 1인 경우 bits에 누적함 | ||
| num = n | ||
| while num != 0 : | ||
| if num % 2 == 1 : | ||
| bits += 1 | ||
| num //= 2 | ||
| return bits |
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,23 @@ | ||
| ''' | ||
| Approach | ||
| - 파이썬에서 제공되는 메소드를 사용해서 문자열 필터링 처리후 왼쪽과 오른쪽 값을 비교 | ||
|
|
||
| Time Complexity: O(N) | ||
| - 문자열 s의 길이를 N이라고 할 때, 문자열을 순회하며 필터링하는 데 O(N) 시간이 걸림 | ||
| - 이후 필터링된 문자열을 뒤집어 right 값을 만드는 데도 O(N) 시간이 걸림 | ||
| - left와 right 값을 비교하는 데도 O(N) 시간이 걸림 | ||
|
|
||
| Space Complexity: O(N) | ||
| - 필터링된 문자열을 저장하는 데 O(N) 공간이 필요함 | ||
| - left와 right 값을 저장하는 데도 각각 O(N) 공간이 필요함 | ||
| ''' | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| # 문자열이 아닌 경우만 소문자 변환 필터링 | ||
| char_str = [char.lower() for char in s if char.isalnum()] | ||
| # 왼쪽 값 | ||
| left = ''.join(char_str) | ||
| # 오른쪽 값 | ||
| right = ''.join(char_str[::-1]) | ||
|
|
||
| return left == right |
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.
저도 해설 보면서 풀이를 하였는데, 쉽지 않았습니다. 스스로 생각하고 풀이를 작성한 것에 박수 드리고 싶습니다. 👏