Files
hello-algo/en/codes/javascript/chapter_dynamic_programming/climbing_stairs_dp.js
T
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

41 lines
1.1 KiB
JavaScript

/**
* File: climbing_stairs_dp.js
* Created Time: 2023-07-26
* Author: yuan0221 ([email protected])
*/
/* Climbing stairs: dynamic programming */
function climbingStairsDP(n) {
if (n === 1 || n === 2) return n;
// Initialize the dp table to store subproblem solutions
const dp = new Array(n + 1).fill(-1);
// Initial states: set the solutions to the smallest subproblems
dp[1] = 1;
dp[2] = 2;
// State transitions: solve progressively larger subproblems using smaller ones
for (let i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
/* Climbing stairs: space-optimized dynamic programming */
function climbingStairsDPComp(n) {
if (n === 1 || n === 2) return n;
let a = 1,
b = 2;
for (let i = 3; i <= n; i++) {
const tmp = b;
b = a + b;
a = tmp;
}
return b;
}
/* Driver code */
const n = 9;
let res = climbingStairsDP(n);
console.log(`Number of ways to climb ${n} stairs: ${res}`);
res = climbingStairsDPComp(n);
console.log(`Number of ways to climb ${n} stairs: ${res}`);