DP
[toc]
OVERVIEW:
Five Steps:
DP Array (DP Table) and Index Meaning:
- The DP array (dp) is a table where each entry dp[i] represents the solution to a subproblem related to the problem you’re solving.
- The index i typically corresponds to some parameter or state in the problem, such as length, position, or another relevant metric.
Recurrence Relation (Recursive Formula):
This is the formula that defines how to compute dp[i] based on previously computed values in the DP table and possibly other input parameters.
For example, if you’re solving a problem related to finding the minimum cost to reach the end of an array, dp[i] might represent the minimum cost to reach index i. The recurrence relation could be something like:
1
2
dp[i] = cost[i] + min(dp[i-1], dp[i-2])This formula states that to compute dp[i], you add the cost at index i to the minimum of the costs of reaching the previous two indices.
Initialization of DP Array:
- Before applying the recurrence relation, you need to initialize the DP array.
- Initialization depends on the problem. Often, it involves setting up base cases that are directly known without computation.
- Example: If dp[0] represents the base case, it might be initialized as dp[0] = 0.
Traversal Order:
- The order in which you populate the DP table matters and is usually sequential.
- Depending on the problem, traversal might be from left to right, right to left, or more complex patterns (like matrix DP).
- Ensure each entry dp[i] depends only on the entries already computed before it.
Example of DP Array Deduction:
- Let’s take an example problem where you’re finding the maximum sum subarray. Given an array nums, dp[i] could represent the maximum sum of a subarray ending at index i.
- Initialization: dp[0] = nums[0] (base case)
- Recurrence relation: dp[i] = max(nums[i], dp[i-1] + nums[i])
- Traversal: From i = 1 to n-1, where n is the length of the array.
Example deduction for array [1, -2, 3, -1, 2]:
Initialize: dp[0] = 1
Compute sequentially:
1
2
3
4scssCopy codedp[1] = max(-2, 1 - 2) = -1
dp[2] = max(3, -1 + 3) = 3
dp[3] = max(-1, 3 - 1) = 2
dp[4] = max(2, 2 + 2) = 4
Therefore, dp array for the maximum sum subarray problem would be [1, -1, 3, 2, 4].
509. Fibonacci Number
Easy
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
1 | F(0) = 0, F(1) = 1 |
Given n, calculate F(n).
Example 1:
1 | Input: n = 2 |
1 | class Solution { |
Meaning of DP Array and Indices:
- The DP array dp is used to store Fibonacci numbers. Specifically,
dp[i]represents the Fibonacci number at index i.
Recurrence Relation (Recursive Formula):
- The problem provides the recurrence relation directly: dp[i] = dp[i - 1] + dp[i - 2].
- This formula states that each Fibonacci number (starting from the third) is the sum of the two preceding Fibonacci numbers.
Initialization of DP Array:
Initialization is provided in the problem statement:
dp[0] = 0; dp[1] = 1;
This initializes the first two Fibonacci numbers, where dp[0] is 0 and dp[1] is 1.
Traversal Order:
- From the recurrence relation dp[i] = dp[i - 1] + dp[i - 2], it’s evident that dp[i] depends on dp[i - 1] and dp[i - 2].
- Therefore, to compute each Fibonacci number up to a given index n, you traverse from i = 2 to n.
70. Climbing Stairs
Easy
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
1 | Input: n = 2 |
1 | class Solution { |
https://programmercarl.com/0070.%E7%88%AC%E6%A5%BC%E6%A2%AF.html#%E6%80%9D%E8%B7%AF
746. Min Cost Climbing Stairs
Easy
You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.
!!!!!!!!!!!!!!!!!!!!!!! You can either start from the step with index 0, or the step with index 1.
Return the minimum cost to reach the top of the floor.
Example 1:
1 | Input: cost = [10,15,20] |
1 | //dp[i] 跳到i上面就立马付钱 (my initial method) |
