Skip to content

Commit 5c80b37

Browse files
authored
Merge pull request #2085 from 8804who/main
[8804who] WEEK 03 solutions
2 parents 8928b2b + 2f2c377 commit 5c80b37

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

combination-sum/8804who.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
dp = {}
4+
5+
for i in range(target+1):
6+
dp[i] = set()
7+
8+
for candidate in candidates:
9+
if candidate <= target:
10+
dp[candidate].add(tuple([candidate]))
11+
12+
for i in range(target+1):
13+
for candidate in candidates:
14+
if i-candidate >= 0:
15+
for j in dp[i-candidate]:
16+
arr = [nums for nums in j]+[candidate]
17+
arr.sort()
18+
dp[i].add(tuple(arr))
19+
20+
answer = [list(nums) for nums in dp[target]]
21+
return answer
22+

decode-ways/8804who.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
dp = [0]*(len(s)+1)
4+
s = '0'+s
5+
dp[0] = 1
6+
for i in range(1, len(s)):
7+
if int(s[i]) == 0:
8+
if int(s[i-1]) != 1 and int(s[i-1]) != 2:
9+
return 0
10+
else:
11+
dp[i]=dp[i-2]
12+
elif int(s[i]) <= 6:
13+
if int(s[i-1]) != 1 and int(s[i-1]) != 2:
14+
dp[i]=dp[i-1]
15+
else:
16+
dp[i]=dp[i-1]+dp[i-2]
17+
else:
18+
if int(s[i-1]) == 1:
19+
dp[i]=dp[i-1]+dp[i-2]
20+
else:
21+
dp[i]=dp[i-1]
22+
return dp[-1]
23+

maximum-subarray/8804who.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def maxSubArray(self, nums: List[int]) -> int:
3+
s = 0
4+
min_s = 0
5+
max_s = nums[0]
6+
7+
for num in nums:
8+
s += num
9+
if max_s < s-min_s:
10+
max_s=s-min_s
11+
if min_s > s:
12+
min_s = s
13+
return max_s
14+

number-of-1-bits/8804who.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
hammingWeight = lambda _, n: n.bit_count()
3+
4+
5+

valid-palindrome/8804who.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import re
2+
class Solution:
3+
def isPalindrome(self, s: str) -> bool:
4+
parsed_string = re.sub('[^a-zA-Z0-9]','',s).upper()
5+
return parsed_string == parsed_string[::-1]
6+

0 commit comments

Comments
 (0)