-
-
Notifications
You must be signed in to change notification settings - Fork 304
[changhyumm] WEEK 03 solutions #2111
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
7 commits
Select commit
Hold shift + click to select a range
e645851
valid-palindrome
changhyumm 216fbcb
number of 1 bits
changhyumm 8dfc886
combination-sum
changhyumm f4b9322
decode-ways
changhyumm b7232f6
decode
changhyumm 0ce6c82
maximum-subarray
changhyumm f4570e8
fix: line indent
changhyumm 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,19 @@ | ||
| class Solution: | ||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| ans = [] | ||
| def combination(index, cur_comb, cur_sum): | ||
| if cur_sum == target: | ||
| ans.append(cur_comb[:]) | ||
| return | ||
| if cur_sum > target or index >= len(candidates): | ||
| return | ||
| cur_comb.append(candidates[index]) | ||
| combination(index, cur_comb, cur_sum + candidates[index]) | ||
| # 합이 target이랑 같던지, 크던지, 길이를 넘던지하면 return으로 탈출 | ||
| # 그 외에 다른 경우의 수를 봐야하므로 | ||
| # 마지막꺼는 다시 빼고 어쨌든 index 넘겨서 다시 combination 확인해봐야함 | ||
| cur_comb.pop() | ||
| combination(index + 1, cur_comb, cur_sum) | ||
| return ans | ||
|
|
||
| return combination(0, [], 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,30 @@ | ||
| class Solution: | ||
| def numDecodings(self, s: str) -> int: | ||
| memo = {} | ||
|
|
||
| def decode(index): | ||
| # 이미 계산했으면 바로 반환 | ||
| if index in memo: | ||
| return memo[index] | ||
|
|
||
| # 기저 사례 | ||
| ## 끝까지 왔으면 성공 | ||
| if index == len(s): | ||
| return 1 | ||
| ## 0으로 시작하면 불가능 | ||
| if s[index] == '0': | ||
| return 0 | ||
|
|
||
| # 재귀 계산 | ||
| ways = decode(index + 1) | ||
|
|
||
| if index + 1 < len(s) and int(s[index:index+2]) <= 26: | ||
| ways += decode(index + 2) | ||
|
|
||
| # 메모이제이션 | ||
| memo[index] = ways | ||
| return ways | ||
|
|
||
| # 시간복잡도, 공간복잡도 O(n) | ||
|
|
||
| return decode(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,14 @@ | ||
| class Solution: | ||
| def maxSubArray(self, nums: List[int]) -> int: | ||
| max_total = nums[0] | ||
| total = 0 | ||
| # subarray는 array에서 서로 인접해야함 | ||
| # 인접한 값의 합이 마이너스인 경우, 그냥 현재 값만 사용하는게 합보다 큼 | ||
| # total 이 0보다 작은경우 그냥 0으로 변경 | ||
| for num in nums: | ||
| if total < 0: | ||
| total = 0 | ||
| total += num | ||
| max_total = max(total, max_total) | ||
| # 시간복잡도 O(n), 공간복잡도 O(1) | ||
| return max_total |
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,11 @@ | ||
| class Solution: | ||
| def hammingWeight(self, n: int) -> int: | ||
| count = 0 | ||
| # 시간복잡도 O(log n) 반으로 나누기 때문 | ||
| while n > 0: | ||
| if n % 2 == 1: | ||
| count += 1 | ||
| n = n // 2 | ||
| else: | ||
| n = n / 2 | ||
|
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.
|
||
| return count | ||
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,11 @@ | ||
| class Solution: | ||
| def isPalindrome(self, s: str) -> bool: | ||
| # 시간복잡도 O(n) | ||
| # loop | ||
| s_list = [x.lower() for x in s if x.isalnum()] | ||
| # 시간복잡도 O(n) | ||
| # loop + list pop() | ||
| for i in range(len(s_list)//2): | ||
| if s_list[i] != s_list.pop(): | ||
| return False | ||
| return True |
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.
코멘트와 코드만으로도 명확하게 이해되는 깔끔한 풀이인 것 같습니다!!
추가로 현재 코드는 memoization + dfs를 이용한 top-down DP로
decode(index)를 계산하기 위해decode(index + 1),decode(index + 2)가 필요한데요,저는 이를 bottom-up DP로 풀이하고,
dp[i]를 계산할 때dp[i - 1],dp[i - 2]만 확인하므로 DP 테이블을 O(1) space 변수 두 개로 대체하여 공간 복잡도를 최적화했습니다!이렇게도 풀 수 있어서 참고차 공유드려요~!