📖 Scenario: You are helping a farmer count the number of ways to climb a staircase with n steps. The farmer can climb either 1 or 2 steps at a time. To avoid repeated work, you will use a technique called memoization to remember results of smaller problems.
🎯 Goal: Build a Java program that uses memoization with top-down dynamic programming to find the number of ways to climb n steps.
📋 What You'll Learn
Create an integer variable n with value 5
Create an integer array memo of size n + 1 initialized with -1
Write a recursive function countWays that takes int step and returns the number of ways to reach step n using memoization
Print the number of ways to climb n steps
💡 Why This Matters
🌍 Real World
Counting ways to climb stairs is like planning steps in a task where some steps depend on previous ones. Memoization helps avoid repeating work.
💼 Career
Memoization and dynamic programming are key skills for software engineers to solve optimization problems efficiently.
Progress0 / 4 steps
1
Create the number of steps variable
Create an integer variable called n and set it to 5.
DSA Java
Hint
Use int n = 5; to create the variable.
2
Create the memo array
Create an integer array called memo of size n + 1 and fill it with -1 to store intermediate results.
DSA Java
Hint
Use a for loop to fill the array with -1.
3
Write the memoized recursive function
Write a recursive function called countWays that takes an integer parameter step and returns an integer. Use memo to store and reuse results. The function should return 1 if step is 0 or 1, and otherwise return the sum of countWays(step - 1) and countWays(step - 2). Use memoization to avoid repeated calculations.
DSA Java
Hint
Use the base cases for step 0 and 1, then check memo before recursive calls.
4
Print the number of ways to climb the stairs
Print the result of calling countWays with n as the argument.
DSA Java
Hint
Use System.out.println(countWays(n)); inside main to print the result.
Practice
(1/5)
1. What is the main purpose of memoization in top-down dynamic programming?
easy
A. To increase the size of the input for better accuracy
B. To sort the input data before processing
C. To store results of subproblems to avoid repeated calculations
D. To convert recursive code into iterative code
Solution
Step 1: Understand memoization role
Memoization saves answers of smaller problems so they are not recalculated.
Step 2: Compare options
Only To store results of subproblems to avoid repeated calculations correctly describes storing subproblem results to avoid repeats.
Final Answer:
To store results of subproblems to avoid repeated calculations -> Option C
Quick Check:
Memoization = store subproblem results [OK]
Hint: Memoization means saving answers to reuse later [OK]
Common Mistakes:
Confusing memoization with sorting
Thinking memoization changes recursion to iteration
Believing memoization increases input size
2. Which of the following is the correct way to declare a memo array for Fibonacci numbers in Java?
easy
A. int memo[] = new int();
B. int memo = new int[n];
C. int[] memo = new int[];
D. int[] memo = new int[n+1];
Solution
Step 1: Check Java array declaration syntax
Correct syntax for array with size n+1 is: int[] memo = new int[n+1];
Step 2: Identify errors in other options
int memo = new int[n]; declares int, not array; C misses size; D uses wrong constructor syntax.
Final Answer:
int[] memo = new int[n+1]; -> Option D
Quick Check:
Array declaration = int[] memo = new int[size] [OK]
Hint: Use int[] memo = new int[n+1] for memo arrays [OK]
Common Mistakes:
Declaring memo as int instead of int[]
Not specifying array size when creating
Using parentheses instead of brackets for arrays
3. What will be the output of the following Java code using memoization for Fibonacci?
public class FibMemo {
static int[] memo;
public static int fib(int n) {
if (n <= 1) return n;
if (memo[n] != -1) return memo[n];
memo[n] = fib(n-1) + fib(n-2);
return memo[n];
}
public static void main(String[] args) {
int n = 5;
memo = new int[n+1];
for (int i = 0; i <= n; i++) memo[i] = -1;
System.out.println(fib(n));
}
}
medium
A. 5
B. 8
C. 3
D. Error due to uninitialized memo
Solution
Step 1: Understand Fibonacci calculation with memo
fib(5) = fib(4) + fib(3), recursively calculated and stored in memo.