Day 29
Five steps to solve dynamic programming problems:
- Determine a
dp
array and the meaning of the array index.
- Determine the recurrence formula.
- Init the
dp
array.
- Determine the recursion order.
- Example derivation
dp
array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public int fib(int n) { if (n <= 1) { return n; }
int a = 0, b = 1;
for (int i = 2; i <= n; i++) { int sum = a + b; a = b; b = sum; } return b; } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| func fib(n int) int { if n <= 1 { return n } a, b := 0, 1 for i := 2; i <= n; i++ { sum := a + b a = b b = sum } return b }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public int climbStairs(int n) { if (n <= 2) { return n; } int[] dp = new int[n+1]; dp[1] = 1; dp[2] = 2; for (int i = 3; i <= n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| func climbStairs(n int) int { if n <= 2 { return n }
dp := make([]int, n+1) dp[1] = 1 dp[2] = 2 for i := 3; i <= n; i++ { dp[i] = dp[i-1] + dp[i-2] } return dp[n] }
|
1 2 3 4 5 6 7 8 9 10 11 12
| class Solution { public int minCostClimbingStairs(int[] cost) { int[] dp = new int[cost.length + 1]; dp[0] = 0; dp[1] = 0;
for (int i = 2; i <= cost.length; i++) { dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]); } return dp[cost.length]; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| func minCostClimbingStairs(cost []int) int { dp := make([]int, len(cost)+1) dp[0], dp[1] = 0, 0
for i := 2; i <= len(cost); i++ { dp[i] = min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]) } return dp[len(cost)] }
func min(a, b int) int { if a < b { return a } return b }
|