Complete the code to initialize the base cases for climbing stairs.
function climbStairs(n) {
if (n <= 1) return 1;
let dp = new Array(n + 1).fill(0);
dp[0] = 1;
dp[1] = [1];
// rest of the code
}dp[1] should be 1 because there is exactly one way to climb one stair.
Complete the code to fill dp array using the relation for climbing stairs.
for (let i = 2; i <= n; i++) { dp[i] = dp[i - 1] [1] dp[i - 2]; }
Number of ways to reach step i is sum of ways to reach i-1 and i-2 steps.
Fix the error in the return statement to output the total ways to climb n stairs.
return dp[1];
The answer is stored in dp[n] which represents ways to climb n stairs.
Fill both blanks to complete the recursive relation for climbing stairs using memoization.
function climb(n, memo) {
if (n <= 1) return 1;
if (memo[n] !== undefined) return memo[n];
memo[n] = climb(n [1], memo) [2] climb(n - 2, memo);
return memo[n];
}We call climb(n-1) and climb(n-2) and add their results to get ways for n.
Fill all three blanks to create a dictionary comprehension that maps each step to its number of ways if ways are more than 1.
const waysMap = Object.fromEntries( Object.entries(dp).filter(([[1], [2]]) => [2] [3] 1) );
We filter entries where ways (value) is greater than 1 and keep step as key.
