Climbing Stairs with Dynamic Programming

🚀 Day 9/50 – LeetCode Challenge 🧩 Climbing Stairs Today’s problem looked simple at first — but it beautifully demonstrates the power of Dynamic Programming. 📌 Problem Summary: You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 step or 2 steps. How many distinct ways can you reach the top? 🧠 Key Insight To reach step n, you can only come from: Step n-1 (taking 1 step) Step n-2 (taking 2 steps) So the formula becomes: ways(n) = ways(n-1) + ways(n-2) This is exactly like the Fibonacci sequence. 🔍 Approach Used (Optimized DP) Instead of using recursion (which is slow), I: ✔️ Used two variables to store previous results ✔️ Iteratively calculated the next value ✔️ Avoided extra array space ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) 💡 Key Learning: ✔️ Recognizing Fibonacci pattern in problems ✔️ Converting recursion into iterative DP ✔️ Optimizing space usage ✔️ Building strong DP fundamentals Simple problem. Powerful concept. Every small step improves problem-solving ability 🚀 🔗 Problem Link: https://lnkd.in/g7mHjHHz #50DaysOfLeetCode #LeetCode #DynamicProgramming #DSA #ProblemSolving #CodingJourney #FutureAIEngineer #Consistency

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories