Climbing Stairs in Python and Java
In this tutorial, we will learn LeetCode #70: Climbing Stairs in very simple language. We will understand the idea step by step, see custom examples, and write complete code in Python and Java.
What Is the Climbing Stairs Problem?
This problem asks us to solve a common coding interview task using the given input. The goal is to return the correct result without using a slow brute force method.
For this problem, we will use dynamic programming because it gives a clean and optimized solution.
Example input: n = 5 Expected result: Output: 8 Explanation: There are 8 ways to climb 5 stairs.
Beginner-Friendly Idea
The main idea is to avoid trying every possible answer blindly. Instead, we keep useful information while reading the input and use that information to make the next decision.
At each step, ask: “What do I already know, and how does the current value change my answer?”
Using our example: n = 5 Approach: dynamic programming We update variables step by step until we reach: Output: 8
Why Do We Use This Approach?
A direct brute force solution is usually easier to think about, but it can become slow when the input is large. The optimized approach keeps only the important state and avoids repeated work.
That is why dynamic programming is useful for this problem.
Step-by-Step Explanation
Let us dry run the algorithm using a custom example.
Step 1 Use this input. n = 5 For 1 stair, ways = 1 For 2 stairs, ways = 2 Step 2 For stair 3, we can come from stair 2 or stair 1. ways(3) = ways(2) + ways(1) ways(3) = 2 + 1 ways(3) = 3 Step 3 For stair 4: ways(4) = ways(3) + ways(2) ways(4) = 3 + 2 ways(4) = 5 Step 4 For stair 5: ways(5) = ways(4) + ways(3) ways(5) = 5 + 3 ways(5) = 8 Final answer = 8
Important Code Logic
The most important part is updating the algorithm state after reading each useful value. This is where the answer becomes better step by step.
Think like this: old_state = what we knew before current_value = value we are checking now new_state = updated result after using current_value For our example, the final state gives: Output: 8
Example 1
Input: n = 5 Output: Output: 8 Explanation: There are 8 ways to climb 5 stairs.
Example 2
Input: n = 6 Output: 13 Explanation: There are 13 different ways to climb 6 stairs.
Python Code
Here is the complete Python solution for LeetCode #70.
class StairClimber:
def climb_stairs(self, n):
if n <= 2:
return n
one_step_before = 2
two_steps_before = 1
for _ in range(3, n + 1):
current = one_step_before + two_steps_before
two_steps_before = one_step_before
one_step_before = current
return one_step_before
climber = StairClimber()
print(climber.climb_stairs(5)) # Output: 8
print(climber.climb_stairs(6)) # Output: 13Java Code
Here is the complete Java solution for LeetCode #70.
class StairClimber {
public int climbStairs(int n) {
if (n <= 2) {
return n;
}
int twoStepsBefore = 1;
int oneStepBefore = 2;
for (int step = 3; step <= n; step++) {
int current = oneStepBefore + twoStepsBefore;
twoStepsBefore = oneStepBefore;
oneStepBefore = current;
}
return oneStepBefore;
}
public static void main(String[] args) {
StairClimber climber = new StairClimber();
System.out.println(climber.climbStairs(5)); // Output: 8
System.out.println(climber.climbStairs(6)); // Output: 13
}
}Time and Space Complexity
Time Complexity: O(n)
The time complexity depends on how many values the algorithm needs to process and whether it uses sorting, binary search, heap, or traversal.
Space Complexity: O(1)
The extra space is used for the variables or data structures needed by the optimized approach.
Final Summary
LeetCode #70: Climbing Stairs becomes easier when we break it into small steps. First understand what the problem asks, then track the important state, dry run with an example, and finally write the code.