Skip to content

[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 7 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions combination-sum/yayyz.py
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
13 changes: 13 additions & 0 deletions decode-ways/yayyz.py
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]
7 changes: 7 additions & 0 deletions maximum-subarray/yayyz.py
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
12 changes: 12 additions & 0 deletions number-of-1-bits/yayyz.py
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 and 연산을해서 값이 있으면 count 값을 증가시켰는데, 생각해보니 and 연산 값 자체를 더해도 되겠네요.
한 수 배우고 갑니다.

n >>= 1
return count

# class Solution:
# def hammingWeight(self, n: int) -> int:
# return bin(n).count('1')
15 changes: 15 additions & 0 deletions valid-palindrome/yayyz.py
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사전에 alpha-numeric 값들만 필터링을 해두는 방법도 좋은 방법 같습니다.
filter 함수를 써보지 못했는데, 적용하신 코드를 참고해서 나중에 써볼 수 있을 것 같습니다. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
25 changes: 25 additions & 0 deletions validate-binary-search-tree/yayyz.py
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