Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 4th/homework_1/id_100/64.MinimumPathSum_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
m = len(grid)
n = len(grid[0])

for i in range(m):
for j in range(n):
if i == 0 and j == 0:
grid[i][j] = grid[i][j]
elif i == 0:
grid[i][j] = grid[i][j] + grid[i][j - 1]
elif j == 0:
grid[i][j] = grid[i][j] + grid[i - 1][j]
else:
grid[i][j] = grid[i][j] + min(grid[i - 1][j], grid[i][j - 1])

return grid[m - 1][n - 1]
18 changes: 18 additions & 0 deletions 4th/homework_2/id_100/121.BestTimetoBuyandSellStock_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if not prices: return 0
profit = 0
min = prices[0]
l = len(prices)
for i in range(l):
if prices[i] < min:
min = prices[i]
else:
if profit < prices[i] - min:
profit = prices[i] - min;

return profit
15 changes: 15 additions & 0 deletions 4th/homework_3/id_100/122.BestTimetoBuyandSellStockII_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
if not prices: return 0
profit = 0
i = 1
while i < len(prices):
if prices[i] - prices[i - 1] > 0:
profit += prices[i] - prices[i - 1]
i = i + 1

return profit
17 changes: 17 additions & 0 deletions 4th/homework_4/id_100/70.ClimbingStairs_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0: return 0
if n == 1: return 1
if n == 2: return 2

res = [0 for i in range(n)]
res[0], res[1] = 1, 2

for i in range(2, n):
res[i] = res[i - 1] + res[i - 2]

return res[n - 1]
17 changes: 17 additions & 0 deletions 4th/homework_5/id_100/198.HouseRobber_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
if n == 0: return 0
if n == 1: return nums[0]
if n == 2: return max(nums[0], nums[1])

nums[0] = nums[0]
nums[1] = max(nums[0], nums[1])
for i in range(2, n):
nums[i] = max(nums[i] + nums[i - 2], nums[i - 1])

return nums[n - 1]
18 changes: 18 additions & 0 deletions 4th/homework_6/id_100/120.Triangle_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""

if not triangle: return 0

n = len(triangle)
for i in range(n, 0, -1):
for j in range(len(triangle[i - 1])):
if i == n:
triangle[i - 1][j] = triangle[i - 1][j]
else:
triangle[i - 1][j] = triangle[i - 1][j] + min(triangle[i][j], triangle[i][j + 1])

return triangle[0][0]
18 changes: 18 additions & 0 deletions 4th/homework_7/id_100/152.MaximumProductSubarray_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution(object):
def maxProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0

result = nums[0]
minNum, maxNum = result, result
for i in range(1, len(nums)):
temp = maxNum
maxNum = max(max(nums[i] * temp, nums[i] * minNum), nums[i])
minNum = min(min(nums[i] * temp, nums[i] * minNum), nums[i])
result = max(maxNum, result)

return result

27 changes: 27 additions & 0 deletions 4th/homework_8/id_100/213.HouseRobberII_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution(object):
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
if n == 0: return 0
if n == 1: return nums[0]
if n == 2: return max(nums[0], nums[1])

pre1 = 0
result1 = 0
for i in range(1, n):
temp = pre1
pre1 = result1
result1 = max(temp + nums[i], pre1)

pre2 = 0
result2 = 0
for i in range(0, n - 1):
temp = pre2
pre2 = result2
result2 = max(temp + nums[i], pre2)

return max(result1, result2)

28 changes: 28 additions & 0 deletions 4th/homework_9/id_100/322.CoinChange_100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# UNDONE
# Status: Time Limit Exceeded


class Solution(object):
def coinChange(self, coins, amount):
"""
:type coins: List[int]
:type amount: int
:rtype: int
"""

if not amount:
return 0

count = amount + 1
for i in range(len(coins)):
curr_val = 0
if amount >= coins[i]:
next_val = self.coinChange(coins, amount - coins[i])
if next_val > -1:
curr_val = 1 + next_val
if curr_val > 0:
count = min(count, curr_val)

if count == amount + 1:
count = -1
return count