Skip to content

Commit fbe7d02

Browse files
committed
day n
1 parent dd4a8f0 commit fbe7d02

10 files changed

+276
-0
lines changed

120. Triangle.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def minimumTotal(self, triangle):
3+
"""
4+
:type triangle: List[List[int]]
5+
:rtype: int
6+
triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
7+
2
8+
3 4
9+
6 5 7
10+
4 1 8 3
11+
12+
"""
13+
dp = [i for i in triangle[-1]]
14+
for i in range(len(triangle) - 1):
15+
current = triangle[len(triangle) - i - 2]
16+
for j in range(len(current)):
17+
dp[j] = min(dp[j], dp[j+1]) + current[j]
18+
return dp[0]
19+
20+
s = Solution()
21+
res = s.minimumTotal([[2],[3,4],[6,5,7],[4,1,8,3]])
22+
print(res)

139. Word Break.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
results = dict()
10+
def findPrefix(substring):
11+
res = []
12+
for i in wordDict:
13+
if substring.startswith(i):
14+
res.append(i)
15+
return res
16+
def recurse(substring):
17+
if substring in results:
18+
return False
19+
if substring == '':
20+
return True
21+
prefixs = findPrefix(substring)
22+
if not prefixs:
23+
results[substring] = False
24+
return False
25+
for prefix in prefixs:
26+
if recurse(substring[len(prefix):]):
27+
return True
28+
results[substring] = False
29+
return False
30+
return recurse(s)
31+
s = Solution()
32+
res = s.wordBreak(s = "catsandog", wordDict = ["cats","dog","sand","and","cat"])
33+
print(res)

198. House Robber.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def rob(self, nums):
3+
if not nums:
4+
return 0
5+
if len(nums) == 1:
6+
return nums[0]
7+
last = 0
8+
current = nums[0]
9+
for i in range(1, len(nums)):
10+
tmp = current
11+
current = max(current, last + nums[i])
12+
last = tmp
13+
return current
14+
s = Solution()
15+
res = s.rob([1,2,3,1])
16+
print(res)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def lengthOfLIS(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
[10,9,2,5,3,7,101,18]
7+
8+
"""
9+
10+
dp = [1] * len(nums)
11+
for i in range(len(nums)):
12+
for j in range(i):
13+
if nums[i] > nums[j]:
14+
dp[i] = max(dp[i], dp[j] + 1)
15+
maxLen = 0
16+
for i in dp:
17+
maxLen = max(maxLen, i)
18+
return maxLen
19+
20+
s = Solution()
21+
res = s.lengthOfLIS([10,9,2,5,3,7,101,18])
22+
print(res)

322. Coin Change.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
minDict = {}
11+
def dp(amount):
12+
if amount == 0:
13+
return 0
14+
if amount < 0:
15+
return -1
16+
if amount in minDict:
17+
if minDict[amount] != -1:
18+
return minDict[amount]
19+
else:
20+
return -1
21+
minSum = float("inf")
22+
for coin in coins:
23+
res = dp(amount - coin)
24+
if res >= 0:
25+
minSum = min(minSum, res)
26+
if minSum != float("inf"):
27+
minDict[amount] = minSum + 1
28+
else:
29+
minDict[amount] = -1
30+
return minDict[amount]
31+
return dp(amount)
32+
33+
s = Solution()
34+
res = s.coinChange([2], 0)
35+
print(res)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def kSmallestPairs(self, nums1, nums2, k):
3+
"""
4+
:type nums1: List[int]
5+
:type nums2: List[int]
6+
:type k: int
7+
:rtype: List[List[int]]
8+
"""
9+
import heapq
10+
smallestHeap = []
11+
output = []
12+
visited = []
13+
heapq.heapify(smallestHeap)
14+
heapq.heappush(smallestHeap, (nums1[0] + nums2[0], 0, 0))
15+
visited.append((0, 0))
16+
while k and smallestHeap:
17+
_, i, j = heapq.heappop(smallestHeap)
18+
output.append([nums1[i], nums2[j]])
19+
if i+1 < len(nums1) and (i+1, j) not in visited:
20+
heapq.heappush(smallestHeap, (nums1[i+1] + nums2[j], i+1, j))
21+
visited.append((i+1, j))
22+
if j+1 < len(nums2) and (i, j+1) not in visited:
23+
heapq.heappush(smallestHeap, (nums1[i] + nums2[j+1], i, j+1))
24+
visited.append((i, j+1))
25+
k -= 1
26+
return output
27+
s = Solution()
28+
res = s.kSmallestPairs(nums1 = [1,7,11], nums2 = [2,4,6], k = 3)
29+
print(res)
30+
31+

5. Longest Palindromic Substring.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution(object):
2+
def longestPalindrome(self, s):
3+
"""
4+
:type s: str
5+
:rtype: str
6+
s = "babad"
7+
dp[start][end] = dp[start+1][end-1] + 2
8+
"""
9+
dp = []
10+
maxLen = 1
11+
maxIdx = (0,0)
12+
for i in range(len(s)):
13+
tmp = [0] * len(s)
14+
dp.append(tmp)
15+
for i in range(len(s)):
16+
# init?
17+
currLen = len(s) - i
18+
for j in range(currLen):
19+
# print(j)
20+
# print(j+i)
21+
if i == 0:
22+
dp[j][j+i] = 1
23+
elif i == 1:
24+
if s[j] == s[j+i]:
25+
dp[j][j+i] = 2
26+
if maxLen < dp[j][j+i]:
27+
maxLen = dp[j][j+i]
28+
maxIdx = (j, j+i)
29+
else:
30+
if dp[j+1][j+i-1] > 0 and s[j] == s[j+i]:
31+
dp[j][j+i] += dp[j+1][j+i-1] + 2
32+
if maxLen < dp[j][j+i]:
33+
maxLen = dp[j][j+i]
34+
maxIdx = (j, j+i)
35+
return s[maxIdx[0]:maxIdx[1]+1]
36+
37+
s = Solution()
38+
res = s.longestPalindrome( "babad")
39+
print(res)
40+
41+

502. IPO.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 findMaximizedCapital(self, k, w, profits, capital):
3+
"""
4+
:type k: int
5+
:type w: int
6+
:type profits: List[int]
7+
:type capital: List[int]
8+
:rtype: int
9+
"""
10+
import heapq
11+
maxProfit = []
12+
minCapital = [(c, p) for c, p in zip(capital, profits)]
13+
heapq.heapify(minCapital)
14+
# c is no decendency oder
15+
for i in range(k):
16+
17+
while minCapital and minCapital[0][0] <= w:
18+
# we get all project we can afford currently
19+
c, p = heapq.heappop(minCapital)
20+
# push them in to heap with order
21+
heapq.heappush(maxProfit, -1 * p)
22+
if not maxProfit:
23+
break
24+
# now, pick up largest profit and add to capital
25+
w += -1 * heapq.heappop(maxProfit)
26+
return w
27+
28+
s = Solution()
29+
res = s.findMaximizedCapital(k = 2, w = 0, profits = [1,2,3], capital = [0,1,1])
30+
print(res)

63. Unique Paths II.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 uniquePathsWithObstacles(self, obstacleGrid):
3+
'''
4+
dp[i] = d[i] + dp2[i-1]
5+
'''
6+
7+
# init first row
8+
m, n = len(obstacleGrid), len(obstacleGrid[0])
9+
dp = [0] * n
10+
if obstacleGrid[0][0] == 0:
11+
dp[0] = 1
12+
for i in range(1, n):
13+
if obstacleGrid[0][i] == 0 and dp[i-1] > 0:
14+
dp[i] = dp[i-1]
15+
for i in range(1, m):
16+
dp2 = [0] * n
17+
for j in range(n):
18+
if obstacleGrid[i][j] == 1:
19+
dp2[j] = 0
20+
else:
21+
if j-1 < 0:
22+
dp2[j] = dp[j]
23+
else:
24+
dp2[j] = dp[j] + dp2[j-1]
25+
dp = dp2
26+
return dp[-1]
27+
s = Solution()
28+
res = s.uniquePathsWithObstacles([[1]])
29+
print(res)
30+

70. Climbing Stairs.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def climbStairs(self, n):
3+
if n == 1:
4+
return 1
5+
if n == 2:
6+
return 2
7+
last = 1
8+
current = 2
9+
for i in range(n-2):
10+
tmp = current
11+
current += last
12+
last = tmp
13+
return current
14+
s = Solution()
15+
res = s.climbStairs(6)
16+
print(res)

0 commit comments

Comments
 (0)