Climbing Stairs Problem: Dynamic Programming Approaches

Practicing Dynamic Programming Through a one of the most popular Problem While practicing problems on LeetCode, I recently worked on the Climbing Stairs problem. At first glance, the problem is simple: You can climb either 1 step or 2 steps at a time. How many distinct ways can you reach the top of n stairs? I started with the most natural idea --> recursion. The recurrence relation is straightforward: f(n) = f(n-1) + f(n-2) Meaning, to reach step n, you must come either from step n-1 or step n-2. But while analyzing the recursion, I noticed something important: many subproblems were being solved repeatedly(it is known as overlapping subproblem). For example, f(3) or f(2) gets calculated multiple times inside the recursion tree. That observation led me to explore Dynamic Programming approaches. I ended up implementing the solution in four different ways: 1. Recursion (Brute Force) Time Complexity: O(2^n) Space Complexity: O(n) (recursion stack) 2. Memoization (Top-Down Dynamic Programming) Time Complexity: O(n) Space Complexity: O(n) 3. Tabulation (Bottom-Up Dynamic Programming) Time Complexity: O(n) Space Complexity: O(n) 4. Space Optimized Dynamic Programming Time Complexity: O(n) Space Complexity: O(1) Since each state only depends on the previous two values, the DP array can be reduced to just two variables. Interestingly, the pattern behind this problem follows the Fibonacci sequence. Problems like this are a great reminder that sometimes the goal is not just to solve the problem, but to understand how different approaches affect efficiency. Still learning, still exploring. If you have suggestions or improvements to this approach, I’d love to hear them. #DynamicProgramming #Algorithms #DSA #LeetCode #Cpp #ProblemSolving

  • text

To view or add a comment, sign in

Explore content categories