Back to Practice Dashboard
Competitive ProgrammingEasy
Count ways to reach nth stair
Learn how to solve the 'Count ways to reach nth stair' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function count_ways_stair(n) that returns the number of distinct ways to climb n stairs. Each time you can either climb 1 or 2 steps.
Constraints
- •1 <= n <= 50
Examples
Example 1
Input
count_ways_stair(3)
Output
3
Explanation
There are three ways: 1+1+1, 1+2, and 2+1.
Example 2
Input
count_ways_stair(4)
Output
5
Explanation
There are five ways: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2.
Need a Hint?
Define subproblem states, establish the recurrence relation, and use memoization (top-down) or tabulation (bottom-up).
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.