Complete the code to return the base case for 1 stair.
if (n == 1) return [1];
When there is only 1 stair, there is exactly 1 way to climb it.
Complete the code to return the base case for 2 stairs.
if (n == 2) return [1];
For 2 stairs, you can climb either two single steps or one double step, so 2 ways.
Fix the error in the recursive return statement to correctly sum the ways.
return climbStairs(n [1] 1) + climbStairs(n [1] 2);
We subtract 1 and 2 from n to find ways to climb remaining stairs.
Fill both blanks to create a memoization check and store result.
if (memo[[1]] != 0) return memo[[1]]; memo[[2]] = climbStairs(n - 1) + climbStairs(n - 2);
We check and store the result for the current stair count n in memo array.
Fill all three blanks to complete the memoized climbStairs method.
public int climbStairs(int n) {
int[] memo = new int[[1]];
return climb(n, memo);
}
private int climb(int n, int[] memo) {
if (n == 1) return 1;
if (n == 2) return 2;
if (memo[[2]] != 0) return memo[[2]];
memo[[3]] = climb(n - 1, memo) + climb(n - 2, memo);
return memo[n];
}The memo array size is n+1 to include index n. We check and store results at memo[n].
