Skip to content

Commit 8220836

Browse files
committed
[:solved] two problems
1 parent ecc6ecc commit 8220836

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

decode-ways/ppxyn1.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# idea : Tree
2+
class Solution:
3+
def numDecodings(self, s: str) -> int:
4+
# recurisve : top-down
5+
# DP : bottom-up can be another way to solve it, but it is not clear for me.
6+
memo = {len(s):1}
7+
def dfs(start):
8+
if start in memo:
9+
return memo[start]
10+
if s[start] == '0':
11+
memo[start] = 0
12+
elif start+1 < len(s) and int(s[start:start+2]) < 27:
13+
memo[start] = dfs(start+1) + dfs(start+2)
14+
else:
15+
memo[start] = dfs(start+1)
16+
return memo[start]
17+
return dfs(0)
18+
19+

maximum-subarray/ppxyn1.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# idea : DP
2+
class Solution:
3+
def maxSubArray(self, nums: List[int]) -> int:
4+
# The solution must run in O(N^2) or better.
5+
'''
6+
1. Idea: Sorting + Two Pointers (x) — This problem does not allow changing the original order of the array.
7+
2. Subarrays (TLE) — O(N^3), too slow.
8+
3. DP (o)
9+
'''
10+
max_total = nums[0]
11+
total = nums[0]
12+
# dp = [0]*len(nums)
13+
# dp[0] = nums[0]
14+
for i in range(1,len(nums)):
15+
# dp[i] = max(nums[i], dp[i-1]+nums[i])
16+
total = max(nums[i], total+nums[i])
17+
max_total = max(total, max_total)
18+
return max_total
19+
20+
21+

0 commit comments

Comments
 (0)