First put the correct answer in the last run
class Solution { public int minCostClimbingStairs(int[] cost) { //Minimum cost of reaching the roof = math.min (minimum cost of reaching the next floor + cost value of the next floor, minimum cost of reaching the second floor) int[] dp = new int[cost.length]; dp[0]=cost[0]; dp[1]=cost[1]; for(int i=2;i<cost.length;i++) { dp[i] = Math.min(dp[i-1],dp[i-2])+cost[i]; } return Math.min(dp[dp.length-1],dp[dp.length-2]); } }
It can be seen from the notes that the thinking of dynamic programming is used. Considering the minimum cost from the current position is an inverse process.
The dynamic programming equation is the minimum cost of reaching the roof = math.min (the minimum cost of reaching the next floor + the cost value of the next floor, and the minimum cost of reaching the second floor).
The boundary value is subscript 0 or subscript 1.
Here are some wrong answers
First kind
class Solution { public int minCostClimbingStairs(int[] cost) { int[] comsumption = new int[2]; comsumption[0] = cost[0]; comsumption[1] = cost[1]; int index0 = 0; int index1 = 1; while(index0 < cost.length-2) { comsumption[0] += cost[index0+1]>=cost[index0+2]?cost[index0+2]:cost[index0+1]; if(cost[index0+1]>=cost[index0+2]) { index0 = index0+2; } else { index0 = index0+1; } } while(index1 < cost.length-2) { comsumption[1] += cost[index1+1]>=cost[index1+2]?cost[index1+2]:cost[index1+1]; if(cost[index1+1]>=cost[index1+2]) { index1 = index1+2; } else { index1 = index1+1; } } return Math.min(comsumption[0],comsumption[1]); } }
Because only the current floor, secondary floor and secondary floor are involved, it is envisaged to use the rolling array comsummary to store the minimum cost of secondary floor and secondary floor. Because it is clearly stated in the question stem that the initial value of the subscript can be selected as 0 or 1, I design to divide it into two different cases. The following is marked as 0 as an example to describe my design idea.
while(index0 < cost.length-2) { comsumption[0] += cost[index0+1]>=cost[index0+2]?cost[index0+2]:cost[index0+1]; if(cost[index0+1]>=cost[index0+2]) { index0 = index0+2; } else { index0 = index0+1; } }
It can be seen that the biggest difference between this part of the code and the code that runs successfully is the meaning of the pointer. In the above code, the pointer is to stand on the step that has spent physical strength to calculate how to select the next step to achieve the minimum cost (1), while the pointer of running the successful code is to stand on the step that has not spent physical strength to review the path and think about how to spend the minimum cost (2). This seems to be the most fatal reason why we can't solve this problem at the beginning. Because I want to design method (1) in this way, the selection of each step can only be the current optimal solution, while method (2) can achieve the optimal solution to achieve the goal.
The failure of using method (1) is as follows:
The minimum cost should be 2, but the optimal solution obtained according to method (1) is 3, and the process is 0 - > 1 - > 2.
After reading some comments, I revised my own ideas and wrote down new error answers.
Second
class Solution { public int minCostClimbingStairs(int[] cost) { int index = cost.length; int value = 0; while(index >= 2) { value += cost[index-1]<cost[index-2]?cost[index-1]:cost[index-2]; if(cost[index-1]<cost[index-2]) { index = index-1; } else { index = index-2; } } return value; } }
The above code is in the form of backstepping. Suppose I'm on the roof and look back to calculate the minimum cost, but this is still a wrong answer.
Use the second wrong method to answer the test case in the figure above. The process is as follows:
Top level - > 1 > 2 with subscript 1
The correct procedure is as follows:
DP [2] = 2 - > DP [3] = 3 - > top layer dp=2
Let's analyze the differences between the two methods:
The subscript of the correct approach is increasing regularly, and the minimum cost is indeed changing dynamically; The second mistake is to synchronize the subscript with the lowest cost. This is because to get the final result, we need to judge every step of the way to select the lowest cost, and the wrong practice is still accumulating the current optimal solution, just changing the direction.
Through the above analysis, dynamic programming needs to traverse each step of judgment to get the results, and the final results need to be related to the previous results.