Skip to content

Commit fbe9f40

Browse files
authored
Merge pull request #1328 from Sung-Heon/main
[Sung-Heon] Week 03 solutions
2 parents 1dab177 + aa8c514 commit fbe9f40

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

Diff for: combination-sum/Sung-Heon.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:
3+
result = []
4+
5+
def backtrack(start: int, target: int, current: list[int]):
6+
if target == 0:
7+
result.append(current[:])
8+
return
9+
10+
for i in range(start, len(candidates)):
11+
if candidates[i] > target:
12+
continue
13+
14+
current.append(candidates[i])
15+
backtrack(i, target - candidates[i], current)
16+
current.pop()
17+
18+
candidates.sort()
19+
backtrack(0, target, [])
20+
return result

Diff for: decode-ways/Sung-Heon.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
if not s or s[0] == '0':
4+
return 0
5+
6+
n = len(s)
7+
dp = [0] * (n + 1)
8+
dp[0] = 1
9+
dp[1] = 1
10+
11+
for i in range(2, n + 1):
12+
if s[i - 1] != '0':
13+
dp[i] += dp[i - 1]
14+
15+
two_digit = int(s[i - 2:i])
16+
if 10 <= two_digit <= 26:
17+
dp[i] += dp[i - 2]
18+
19+
return dp[n]

Diff for: maximum-subarray/Sung-Heon.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maxSubArray(self, nums: list[int]) -> int:
3+
max_sum = nums[0]
4+
current_sum = nums[0]
5+
6+
for num in nums[1:]:
7+
current_sum = max(num, current_sum + num)
8+
max_sum = max(max_sum, current_sum)
9+
10+
return max_sum

Diff for: number-of-1-bits/Sung-Heon.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
count = 0
4+
while n:
5+
count += n & 1
6+
n >>= 1
7+
return count

Diff for: valid-palindrome/Sung-Heon.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
filtered = ''.join(char.lower() for char in s if char.isalnum())
4+
end = len(filtered) - 1
5+
if end <= 0:
6+
return True
7+
start = 0
8+
while True:
9+
end_s = filtered[end]
10+
start_s = filtered[start]
11+
if end_s == start_s:
12+
end -= 1
13+
start += 1
14+
15+
16+
else:
17+
return False
18+
if start >= end:
19+
return True
20+
if end <= 0:
21+
return True
22+
continue
23+
24+

0 commit comments

Comments
 (0)