Leetcode Dynamic Programming Problem Collection (1)
Green hand in DP problem
The key of Dynamic Programming is to get local result, then get global result.
- 70.Climbing Stairs
- 121.Best Time to Buy and Sell Stock *
- 53.Maximum Subarray
- 198.House Robber
- 746.Min Cost Climbing Stairs
- 42.Trapping Rain Water
70. Climbing Stairs
Tried to use recursion but it was super slow. Use iteration is much faster.
calculate the previous step 1 and step 2, keep tracking it. Time = O(n)
1 | def climbStairs(self, n: int) -> int: |
121. Best Time to Buy and Sell Stock
I like this one. logging the lowest buying price while updating max profit1
2
3
4
5
6
7
8
9def maxProfit(self, prices: List[int]) -> int:
if len(prices) < 1:
return 0
lowest = prices[0]
profit = 0
for price in prices:
lowest = min(lowest, price)
profit = max(profit, price - lowest)
return profit
53. Maximum Subarray
Greedy
find local max sum, update global max sum if greater.1
2
3
4
5
6
7
8def maxSubArray(self, nums: List[int]) -> int:
max_sum = nums[0]
cur_sum = nums[0]
for num in nums[1:]:
cur_sum = max(cur_sum + num, num)
max_sum = max(cur_sum, max_sum)
return max_sum
Dynamic Programming
1 | def maxSubArray(self, nums: List[int]) -> int: |
198. House Robber
1 | def rob(self, nums: List[int]) -> int: |
746. Min Cost Climbing Stairs
Either pay the last step, or pay the second last step1
2
3
4
5def minCostClimbingStairs(self, cost: List[int]) -> int:
s0, s1 = cost[0], cost[1]
for i in range(2, len(cost)):
s1, s0 = min(cost[i] + s0, cost[i] + s1), s1
return min(s0, s1)
42. Trapping Rain Water
DP solution, log left max, then log right max1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21def trap(self, height: List[int]) -> int:
if len(height) < 3:
return 0
left_max, right_max = 0, 0
# left, right, rain
dp = [[0, 0] for _ in range(len(height))]
# find left max:
for i, h in enumerate(height):
dp[i][0] = left_max
left_max = max(h, left_max)
res = 0
# find right max, calculate rain
for i in range(len(height) - 1, -1, -1):
dp[i][1] = right_max
right_max = max(right_max, height[i])
res += max(min(dp[i][0], dp[i][1]) - height[i], 0)
return res
It also has a stack solution.