-
-
Notifications
You must be signed in to change notification settings - Fork 304
[Donghae0230] WEEK 03 solutions #2118
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
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,21 @@ | ||
| # 문제 풀이 | ||
| # 모든 경우의 수를 탐색하기 위해 백트래킹 사용 | ||
| # - 현재 조합의 합이 target보다 크면 종료 | ||
| # - 현재 조합의 합이 target과 같으면 결과에 추가 | ||
|
|
||
| class Solution: | ||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| def backtrack(start, combination): | ||
| if sum(combination) > target: | ||
| return | ||
| if sum(combination) == target: | ||
| result.append(combination[:]) | ||
| return | ||
| for i in range(start, len(candidates)): | ||
| combination.append(candidates[i]) | ||
| backtrack(i, combination) | ||
| combination.pop() | ||
|
|
||
| result = [] | ||
| backtrack(0, []) | ||
| 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,21 @@ | ||
| # 문제 풀이 | ||
| # 1. 입력값 n을 binary 형태로 변환 | ||
| # - 입력값 n이 1보다 크면 2로 나눠 몫과 나머지 계산 | ||
| # - 나머지를 리스트에 추가해 반환 | ||
| # 2. 반환된 리스트에서 1의 갯수 반환 | ||
|
|
||
| # 시간복잡도 O(log n): n을 2로 나누면서 재귀 함수 실행 | ||
| # 공간복잡도 O(log n): 비트를 저장하는 리스트의 길이 | ||
|
|
||
| class Solution: | ||
| def devide_by_2 (self, n, temp): | ||
| if n > 1 : | ||
| temp.append(n % 2) | ||
| return self.devide_by_2(n // 2, temp) | ||
|
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.
|
||
| temp.append(1) | ||
| return n, temp | ||
|
|
||
| def hammingWeight(self, n: int) -> int: | ||
| result = [] | ||
| n, result = self.devide_by_2(n, result) | ||
| return result.count(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,16 @@ | ||
| # 문제 풀이 | ||
| # 1. 문자열을 소문자로 변환 후 문자와 숫자가 아닌 값을 제거 | ||
| # 2. 문자열을 뒤집은 후 원래 문자열과 비교 | ||
|
|
||
| # 시간복잡도 O(n): 문자열 처리(re.sub, reversed 등) 사용 | ||
| # 공간복잡도 O(n): 원래 문자열 만큼의 공간 사용 | ||
| import re | ||
|
|
||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| cleaned_s = re.sub(r'[^a-zA-Z0-9]', '', s.lower()) | ||
| reversed_s = ''.join(reversed(cleaned_s)) | ||
| if cleaned_s == reversed_s: | ||
| return True | ||
| else: | ||
| return False |
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.
가독성 좋은 백트래킹 풀이인 것 같습니다!
여기에 추가로
candidates를 정렬한다면 pruning을 적용할 수 있어 더 최적화 된 풀이가 가능할 것 같아요~!