Skip to content

Commit b1d3e60

Browse files
committed
[Leo] 11th Week solutions (4,5)
1 parent 551fd84 commit b1d3e60

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

maximum-product-subarray/Leo.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def maxProduct(self, nums: List[int]) -> int:
3+
curMax, curMin = 1, 1
4+
res = nums[0]
5+
6+
for n in nums:
7+
vals = (n, n * curMax, n * curMin)
8+
curMax, curMin = max(vals), min(vals)
9+
10+
res = max(res, curMax)
11+
12+
return res
13+
14+
## TC: O(n), SC: O(1)

word-break/Leo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
3+
dp = [True] + [False] * len(s)
4+
5+
for i in range(1, len(s) + 1):
6+
for w in wordDict:
7+
if i - len(w) >= 0 and dp[i - len(w)] and s[:i].endswith(w):
8+
dp[i] = True
9+
10+
return dp[-1]
11+
12+
## TC: O(len(str) * len(wordDict) * len(avg(wordDict[n])))
13+
## SC: O(n)

0 commit comments

Comments
 (0)