分析,数组长度为i,则走到楼顶步数为i+1; PS:cost[i]记录的是,从i楼往上走一楼或二楼需要消耗的体力
上0层 dp[0] = 0; //不动
上零层楼顶 dp[1] = 0; // 到达cost[1]的位置 {10} A.length=1 到1楼楼顶cost[1] 二楼
上一层楼顶 dp[2] = Min(dp[0] +cost[0],dp[1]+cost[1]) = 10 // 到达cost[2]的位置 此时输入为{10,15} A.length=2,2楼楼顶三楼
一步到 cost[0],再走两步 0, 1 , 2 , 3
dp[3] = Min(dp[1] +cost[1],dp[2]+cost[2]) = 15 {10,15,20} 两步cost[1],再两步到楼顶cost[3]
上N层楼顶 dp[N] = Min(dp[N-2] +cost[N-2],dp[N-1]+cost[N-1]) N+1楼 A.length=N,N楼楼顶N+1楼
public static int minCostClimbingStairs(int[] cost) { int[] dp = new int[cost.length+1]; dp[0]=0; dp[1]=0; for(int i=2;i<cost.length+1;i++){ System.out.println((dp[i-2]+cost[i-2])+" , " +(dp[i-1]+cost[i-1])); dp[i] = Math.min(dp[i-2]+cost[i-2],dp[i-1]+cost[i-1]); } return dp[cost.length]; }
