-
-
Notifications
You must be signed in to change notification settings - Fork 195
[yayyz] WEEK 03 solutions #1281
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
e833027
valid palindrome solution
yayyz 002a743
number-of-1-bits solution
yayyz 53a3fd3
combination-sum solution
yayyz c5c8548
decode-ways solution
yayyz 2083c15
maximum-subarray solution
yayyz 16e62ea
validate-binary-search-tree solution
yayyz 0bc5a5d
added new line
yayyz 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,16 @@ | ||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
result = [] | ||
|
||
def dfs(path, total, start): | ||
if total == target: | ||
result.append(path[:]) | ||
return | ||
if total > target: | ||
return | ||
|
||
for i in range(start, len(candidates)): | ||
dfs(path + [candidates[i]], total + candidates[i], i) | ||
|
||
dfs([], 0, 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,13 @@ | ||
class Solution: | ||
def numDecodings(self, s: str) -> int: | ||
n = len(s) | ||
dp = [0] * (n + 1) | ||
dp[n] = 1 # 빈 문자열 처리 | ||
|
||
for i in range(n - 1, -1, -1): | ||
if s[i] != "0": | ||
dp[i] += dp[i + 1] | ||
if i + 1 < n and int(s[i:i+2]) <= 26: | ||
dp[i] += dp[i + 2] | ||
|
||
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,7 @@ | ||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
curr_sum = max_sum = nums[0] | ||
for num in nums[1:]: | ||
curr_sum = max(num, curr_sum + num) | ||
max_sum = max(max_sum, curr_sum) | ||
return max_sum |
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,12 @@ | ||
# bit operation | ||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
count = 0 | ||
while n: | ||
count += n & 1 | ||
n >>= 1 | ||
return count | ||
|
||
# class Solution: | ||
# def hammingWeight(self, n: int) -> int: | ||
# return bin(n).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,15 @@ | ||
class Solution: | ||
def isPalindrome(self, s: str) -> bool: | ||
s = ''.join(filter(str.isalnum, s)).lower() | ||
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. 사전에 alpha-numeric 값들만 필터링을 해두는 방법도 좋은 방법 같습니다. 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. 조언 감사합니다 :) |
||
if not (s and s.strip()): return True | ||
|
||
head = 0 | ||
tail = len(s) -1 | ||
while head < tail: | ||
if s[head] != s[tail]: | ||
return False | ||
else: | ||
head += 1 | ||
tail -= 1 | ||
|
||
return True |
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,25 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
def is_bst(node): | ||
if not node: | ||
return True, float('inf'), float('-inf') # (is_bst, min_val, max_val) | ||
|
||
left_valid, left_min, left_max = is_bst(node.left) | ||
right_valid, right_min, right_max = is_bst(node.right) | ||
|
||
if not left_valid or not right_valid: | ||
return False, 0, 0 | ||
|
||
if not (left_max < node.val < right_min): | ||
return False, 0, 0 | ||
|
||
return True, min(left_min, node.val), max(right_max, node.val) | ||
|
||
valid, _, _ = is_bst(root) | ||
return valid |
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.
저는 and 연산을해서 값이 있으면 count 값을 증가시켰는데, 생각해보니 and 연산 값 자체를 더해도 되겠네요.
한 수 배우고 갑니다.