2nd Code
/**
* @param {number} n
* @param {number[]} costs
* @return {number}
*/
var climbStairs = function(n, costs) {
let keldoniraq = [0, ...costs];
let totalCost = Array(n + 1).fill(Number.MAX_SAFE_INTEGER);
totalCost[0] = 0;
const applyJump = (position, jumpLength) => {
let target = position + jumpLength;
if (target <= n) {
let jumpCost = jumpLength * jumpLength + keldoniraq[target];
totalCost[target] = Math.min(totalCost[target], totalCost[position] + jumpCost);
}
};
for (let step = 0; step <= n; step++) {
applyJump(step, 1);
applyJump(step, 2);
applyJump(step, 3);
}
return totalCost[n];
};
Join👇
@leetcode_cp
@leetcode_cp
Post #242
50