Skip to content

[Sung-Heon] Week 03 solutions #1328

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 2 commits into from
Apr 21, 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
20 changes: 20 additions & 0 deletions combination-sum/Sung-Heon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution:
def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:
result = []

def backtrack(start: int, target: int, current: list[int]):
if target == 0:
result.append(current[:])
return

for i in range(start, len(candidates)):
if candidates[i] > target:
continue

current.append(candidates[i])
backtrack(i, target - candidates[i], current)
current.pop()

candidates.sort()
backtrack(0, target, [])
return result
19 changes: 19 additions & 0 deletions decode-ways/Sung-Heon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def numDecodings(self, s: str) -> int:
if not s or s[0] == '0':
return 0

n = len(s)
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1

for i in range(2, n + 1):
if s[i - 1] != '0':
dp[i] += dp[i - 1]

two_digit = int(s[i - 2:i])
if 10 <= two_digit <= 26:
dp[i] += dp[i - 2]

return dp[n]
10 changes: 10 additions & 0 deletions maximum-subarray/Sung-Heon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def maxSubArray(self, nums: list[int]) -> int:
max_sum = nums[0]
current_sum = nums[0]

for num in nums[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)

return max_sum
7 changes: 7 additions & 0 deletions number-of-1-bits/Sung-Heon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def hammingWeight(self, n: int) -> int:
count = 0
while n:
count += n & 1
n >>= 1
return count
24 changes: 24 additions & 0 deletions valid-palindrome/Sung-Heon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
filtered = ''.join(char.lower() for char in s if char.isalnum())
end = len(filtered) - 1
if end <= 0:
return True
start = 0
while True:
end_s = filtered[end]
start_s = filtered[start]
if end_s == start_s:
end -= 1
start += 1


else:
return False
if start >= end:
return True
if end <= 0:
return True
continue