Skip to content

Commit 649e8fe

Browse files
author
Your Name
committed
day 14
1 parent dfbed2b commit 649e8fe

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

139WordBreak.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def wordBreak(self, s, wordDict):
3+
"""
4+
:type s: str
5+
:type wordDict: List[str]
6+
:rtype: bool
7+
s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
8+
"""
9+
from collections import defaultdict
10+
mymap = defaultdict()
11+
mymap[0] = True
12+
def helper(end):
13+
if end in mymap:
14+
return mymap[end]
15+
res = False
16+
for i in wordDict:
17+
if s[0:end].endswith(i):
18+
res = res or helper(end - len(i))
19+
if res:
20+
mymap[end] = res
21+
return res
22+
mymap[end] = res
23+
return res
24+
res = helper(len(s))
25+
return res
26+
27+
sol = Solution()
28+
res = sol.wordBreak(s = "applepenapple", wordDict = ["apple","pen"])
29+
print(res)

198HouseRobber.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def rob(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
[2,7,9,3,1]
7+
12
8+
2 -> 2
9+
2 7 -> 7
10+
2 7 9 -> 11
11+
2 7 9 3 -> 11
12+
"""
13+
if len(nums) == 1:
14+
return nums[0]
15+
from collections import defaultdict
16+
self.map = defaultdict()
17+
self.map[0] = nums[0]
18+
self.map[1] = max(nums[0], nums[1])
19+
def helper(n):
20+
# must rob last
21+
# don't rob last
22+
if n in self.map:
23+
return self.map[n]
24+
res = max(helper(n-1), helper(n-2) + nums[n])
25+
self.map[n] = res
26+
return res
27+
return helper(len(nums) - 1)
28+
sol = Solution()
29+
res = sol.rob([2, 7, 9, 3, 1])
30+
print(res)

322.CoinChange.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution(object):
2+
def coinChange(self, coins, amount):
3+
"""
4+
:type coins: List[int]
5+
:type amount: int
6+
:rtype: int
7+
coins = [1,2,5], amount = 11
8+
5 + 5 + 1
9+
"""
10+
if amount == 0:
11+
return 0
12+
from collections import defaultdict
13+
mymap = defaultdict()
14+
for i in coins:
15+
mymap[i] = 1
16+
minval = min(coins)
17+
def helper(total):
18+
if total in mymap:
19+
return mymap[total]
20+
if total < minval:
21+
return -1
22+
res = float("inf")
23+
for i in coins:
24+
tmp = helper(total - i)
25+
if tmp == -1:
26+
continue
27+
res = min(res, tmp + 1)
28+
mymap[total] = res
29+
if res == float("inf"):
30+
return -1
31+
return res
32+
return helper(amount)
33+
34+
sol = Solution()
35+
res = sol.coinChange([2], 3)
36+
print(res)

70ClimbingStairs.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def __init__(self):
3+
from collections import defaultdict
4+
self.map = defaultdict()
5+
self.map[1] = 1
6+
self.map[2] = 2
7+
def climbStairs(self, n):
8+
"""
9+
:type n: int
10+
:rtype: int
11+
"""
12+
if n in self.map:
13+
return self.map[n]
14+
res = self.climbStairs(n-1) + self.climbStairs(n - 2)
15+
self.map[n] = res
16+
return res
17+
18+
19+
sol = Solution()
20+
res = sol.climbStairs(10)
21+
print(res)

0 commit comments

Comments
 (0)