12345678910111213141516171819202122232425// cost: [10,15,20]// 输出: 15// cost: [1,100,1,1,1,100,1,1,100,1]// 输出: 6// dp[i] 定义为 到 i 位置的最小花费// 状态方程: dp[i] = min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2])// 初始值: dp[i]=0, dp[0]=cost[0], dp[1]=min(cost[0],cost[1]) /** * @param {number[]} cost * @return {number} */var minCostClimbingStairs = function(cost) { let n = cost.length; let dp = new Array(n+1).fill(0); let minCost = dp[0]; dp[0] = 0; dp[1] = 0; for(let i=2;i<=n;i++){ dp[i] = Math.min(dp[i-1]+cost[i-1], dp[i-2]+cost[i-2]); } return dp[n];};