Skip to content

Commit ec473c1

Browse files
author
zhangbingxu
committed
homework-4
1 parent b534b77 commit ec473c1

File tree

9 files changed

+179
-0
lines changed

9 files changed

+179
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def minPathSum(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
m = len(grid)
8+
n = len(grid[0])
9+
10+
for i in range(m):
11+
for j in range(n):
12+
if i == 0 and j == 0:
13+
grid[i][j] = grid[i][j]
14+
elif i == 0:
15+
grid[i][j] = grid[i][j] + grid[i][j - 1]
16+
elif j == 0:
17+
grid[i][j] = grid[i][j] + grid[i - 1][j]
18+
else:
19+
grid[i][j] = grid[i][j] + min(grid[i - 1][j], grid[i][j - 1])
20+
21+
return grid[m - 1][n - 1]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def maxProfit(self, prices):
3+
"""
4+
:type prices: List[int]
5+
:rtype: int
6+
"""
7+
if not prices: return 0
8+
profit = 0
9+
min = prices[0]
10+
l = len(prices)
11+
for i in range(l):
12+
if prices[i] < min:
13+
min = prices[i]
14+
else:
15+
if profit < prices[i] - min:
16+
profit = prices[i] - min;
17+
18+
return profit
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def maxProfit(self, prices):
3+
"""
4+
:type prices: List[int]
5+
:rtype: int
6+
"""
7+
if not prices: return 0
8+
profit = 0
9+
i = 1
10+
while i < len(prices):
11+
if prices[i] - prices[i - 1] > 0:
12+
profit += prices[i] - prices[i - 1]
13+
i = i + 1
14+
15+
return profit
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def climbStairs(self, n):
3+
"""
4+
:type n: int
5+
:rtype: int
6+
"""
7+
if n == 0: return 0
8+
if n == 1: return 1
9+
if n == 2: return 2
10+
11+
res = [0 for i in range(n)]
12+
res[0], res[1] = 1, 2
13+
14+
for i in range(2, n):
15+
res[i] = res[i - 1] + res[i - 2]
16+
17+
return res[n - 1]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def rob(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
n = len(nums)
8+
if n == 0: return 0
9+
if n == 1: return nums[0]
10+
if n == 2: return max(nums[0], nums[1])
11+
12+
nums[0] = nums[0]
13+
nums[1] = max(nums[0], nums[1])
14+
for i in range(2, n):
15+
nums[i] = max(nums[i] + nums[i - 2], nums[i - 1])
16+
17+
return nums[n - 1]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def minimumTotal(self, triangle):
3+
"""
4+
:type triangle: List[List[int]]
5+
:rtype: int
6+
"""
7+
8+
if not triangle: return 0
9+
10+
n = len(triangle)
11+
for i in range(n, 0, -1):
12+
for j in range(len(triangle[i - 1])):
13+
if i == n:
14+
triangle[i - 1][j] = triangle[i - 1][j]
15+
else:
16+
triangle[i - 1][j] = triangle[i - 1][j] + min(triangle[i][j], triangle[i][j + 1])
17+
18+
return triangle[0][0]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def maxProduct(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if not nums: return 0
8+
9+
result = nums[0]
10+
minNum, maxNum = result, result
11+
for i in range(1, len(nums)):
12+
temp = maxNum
13+
maxNum = max(max(nums[i] * temp, nums[i] * minNum), nums[i])
14+
minNum = min(min(nums[i] * temp, nums[i] * minNum), nums[i])
15+
result = max(maxNum, result)
16+
17+
return result
18+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def rob(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
n = len(nums)
8+
if n == 0: return 0
9+
if n == 1: return nums[0]
10+
if n == 2: return max(nums[0], nums[1])
11+
12+
pre1 = 0
13+
result1 = 0
14+
for i in range(1, n):
15+
temp = pre1
16+
pre1 = result1
17+
result1 = max(temp + nums[i], pre1)
18+
19+
pre2 = 0
20+
result2 = 0
21+
for i in range(0, n - 1):
22+
temp = pre2
23+
pre2 = result2
24+
result2 = max(temp + nums[i], pre2)
25+
26+
return max(result1, result2)
27+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# UNDONE
2+
# Status: Time Limit Exceeded
3+
4+
5+
class Solution(object):
6+
def coinChange(self, coins, amount):
7+
"""
8+
:type coins: List[int]
9+
:type amount: int
10+
:rtype: int
11+
"""
12+
13+
if not amount:
14+
return 0
15+
16+
count = amount + 1
17+
for i in range(len(coins)):
18+
curr_val = 0
19+
if amount >= coins[i]:
20+
next_val = self.coinChange(coins, amount - coins[i])
21+
if next_val > -1:
22+
curr_val = 1 + next_val
23+
if curr_val > 0:
24+
count = min(count, curr_val)
25+
26+
if count == amount + 1:
27+
count = -1
28+
return count

0 commit comments

Comments
 (0)