LeetCode 70: Climbing Stairs with Recursion and DP

LeetCode 70 — Climbing Stairs Worked on finding the number of distinct ways to reach the top when you can climb either 1 or 2 steps at a time. Initially, it looks like a simple counting problem. But structurally, it follows a clear recurrence pattern. Approach — Recurrence Relation - If n ≤ 1 → only one possible way - From step n, you can: - Take 1 step → reduce to (n - 1) - Take 2 steps → reduce to (n - 2) - Total ways = ways(n - 1) + ways(n - 2) This is essentially the Fibonacci pattern in a different form. While testing with small inputs, the recursive solution worked correctly. However, on submission it resulted in Time Limit Exceeded for larger inputs. That made one thing very clear :- The logic is correct, but overlapping subproblems make the pure recursive approach inefficient. Key learning :- Correct logic is not enough. Efficiency matters. Recognizing when recursion needs optimization (memoization / DP) is just as important as writing it. #leetcode #recursion #dynamicprogramming #dsa #algorithms #problemSolving #codingjourney

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories