Skip to content

Commit 7157d23

Browse files
authored
[daehyun99] WEEK 03 Solutions (#2738)
* Solve: isPalindrome * Solve: hammingWeight * Solve: combinationSum * Solve: numDecodings * Solve: maxSubArray
1 parent da9f37f commit 7157d23

5 files changed

Lines changed: 80 additions & 0 deletions

File tree

combination-sum/daehyun99.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import deque
2+
class Solution:
3+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
4+
result = []
5+
candidates = sorted(candidates)
6+
que = deque([candidate] for candidate in candidates)
7+
while len(que) > 0:
8+
values = que.popleft()
9+
sum_values = sum(values)
10+
if sum_values == target:
11+
result.append(values)
12+
continue
13+
for candidate in candidates:
14+
if values[-1] > candidate:
15+
continue
16+
if sum_values + candidate <= target:
17+
temp = values.copy()
18+
temp.append(candidate)
19+
que.append(temp)
20+
return result

decode-ways/daehyun99.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from collections import deque
2+
class Solution:
3+
def numDecodings(self, s: str) -> int:
4+
decoded = set([str(i) for i in range(1, 27)])
5+
counts = [0 for i in range(len(s))]
6+
if len(s) == 1:
7+
if s == "0":
8+
return 0
9+
else:
10+
return 1
11+
12+
if s[0] in decoded:
13+
counts[0] += 1
14+
if s[1] in decoded:
15+
counts[1] += counts[0]
16+
if s[0:2] in decoded:
17+
counts[1] += 1
18+
19+
for i in range(2, len(s)):
20+
if s[i] in decoded:
21+
counts[i] += counts[i-1]
22+
if s[i-1:i+1] in decoded:
23+
counts[i] += counts[i-2]
24+
return counts[-1]
25+

maximum-subarray/daehyun99.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def maxSubArray(self, nums: List[int]) -> int:
3+
if max(nums) <= 0:
4+
return max(nums)
5+
6+
result = max(nums)
7+
val = 0
8+
for i in range(len(nums)-1, -1, -1):
9+
val += nums[i]
10+
if val < 0:
11+
val = 0
12+
result = max(result, val)
13+
14+
val = 0
15+
for i in range(len(nums)):
16+
val += nums[i]
17+
if val < 0:
18+
val = 0
19+
result = max(result, val)
20+
return result

number-of-1-bits/daehyun99.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def hammingWeight(self, n: int) -> int:
3+
return n.bit_count()

valid-palindrome/daehyun99.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def isPalindrome(self, s: str) -> bool:
3+
forward = ""
4+
5+
s = s.lower()
6+
7+
for string in s:
8+
if 97 <= ord(string) <= 122 or 48 <= ord(string) <= 57:
9+
forward += string
10+
backward = forward[::-1]
11+
12+
return True if forward == backward else False

0 commit comments

Comments
 (0)