第七十五天:力扣746题,使用最小花费爬楼梯
地址:leetcode-cn.com/problems/mi…
思路:动态规划,每一级的最小值,都是前一级或者前二级最小值加上来的,所以动态规划可以轻易解决。
function minCostClimbingStairs(cost: number[]): number {
let res:Array<number> = new Array(cost.length + 1);
res[0] = 0;
res[1] = 0;
for(let i:number = 2; i < cost.length + 1; i++)
{
res[i] = Math.min(res[i - 1] + cost[i - 1], res[i - 2] + cost[i - 2]);
}
return res[cost.length];
};
执行用时:96 ms, 在所有 TypeScript 提交中击败了66.67%的用户
内存消耗:40.8 MB, 在所有 TypeScript 提交中击败了38.89%的用户