🚀 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
Climbing Stairs with Dynamic Programming
More Relevant Posts
-
🚀 Day 139 – LeetCode 150 Days Challenge 📌 Problem: Triangle Minimum Path Sum Today’s problem is a classic example of Dynamic Programming (Bottom-Up Approach). 🧠 Problem Understanding You are given a triangle array, and your task is to find the minimum path sum from top to bottom. 👉 At each step, you can move to: Same index (j) Next index (j+1) in the next row 💡 Key Insight Instead of trying all possible paths (which is expensive 😵), we solve it bottom-up. ✔ Start from the last row (base case) ✔ Move upwards and calculate minimum path for each cell ✔ Store results in a DP table to avoid recomputation ⚙️ Approach Copy last row into DP (this is our base case) Move from second last row → top For each cell: Take minimum of two possible moves Add current value 🔁 Transition Formula dp[i][j] = triangle[i][j] + min(dp[i+1][j], dp[i+1][j+1]) ⏱️ Complexity 🟢 Time: O(n²) 🟢 Space: O(n²) 🔥 Takeaway This problem teaches: How to convert recursion → DP Importance of solving from base case upward Space-time tradeoffs in DP Consistency is the key 🔑 Let’s keep pushing towards mastering DSA 💪 #Day139 #LeetCode #DSA #DynamicProgramming #CodingJourney #150DaysOfCode
To view or add a comment, sign in
-
-
🚀 March LeetCode Challenge: Day 05/31 | The Power of Parity in the March LeetCode Challenge! One thing I love about competitive programming is finding "The Shortcut." Today, for LeetCode 1758: Minimum Changes To Make Alternating Binary String, I was able to turn a string manipulation problem into a simple mathematical observation. 🧩 The Challenge Given a binary string, what is the minimum number of character flips needed to make it "alternating" (e.g., 0101... or 1010...)? 💡 My Logic: One Loop, Two Results Initially, you might think you need to check the string against two different patterns separately. But there's a more elegant way: The Pattern Rule: In an alternating string starting with 0, the character at index i should be 0 if i is even, and 1 if i is odd. This can be represented simply as i % 2. The Complement: If it takes X operations to make the string start with 0, it will take exactly (Total Length - X) operations to make it start with 1. By calculating one, I automatically knew the other. Taking the min() of these two gave me the optimal answer in a single pass! 📊 Performance Reflection Time Complexity: O(N) – A single traversal of the string. Space Complexity: O(1) – No extra strings created, just a single counter. #LeetCode #CodingChallenge #DynamicProgramming #Cpp #SoftwareEngineering #ProblemSolving #MarchCodingChallenge #VITBhopal #DataStructures #DrGViswanathan #VIT
To view or add a comment, sign in
-
-
Heaps and K-Related Problems Day 217 Today Today is day 217 of my coding challenge. I focused on using Heaps and Priority Queues to solve problems more efficiently. A very important rule I learned today is that when a question asks for the kth smallest or kth largest element, you should use a heap. The trick is to keep the heap size restricted to K. This makes the code much faster than simple sorting. I solved LeetCode 215 which is about finding the Kth largest element in an array. Instead of sorting the whole array which takes O(n log n), I used a Min-Priority Queue of size K. This reduced the time complexity to O(n log k). I also worked on LeetCode 703 where I had to find the kth largest element in a data stream. A Min-Heap is perfect here because the smallest element in a heap of size K is always the kth largest overall. For LeetCode 1046 called Last Stone Weight, the simple way uses repeated sorting which is very slow. I optimized it using a Max-Priority Queue to get a speed of O(n log n) because it always keeps the heaviest stones at the top. Another interesting problem was LeetCode 347, Top K Frequent Elements. I used a frequency map and then a Min-Priority Queue to keep only the top K frequent items. This is better than sorting because it avoids sorting elements we do not need. Finally I solved LeetCode 378 which is the Kth Smallest Element in a Sorted Matrix. Using a Min-Heap helped me find the right value efficiently by only looking at the necessary rows and columns. Using heaps is a game changer for performance especially when dealing with large amounts of data. LeetCode Questions i solved: LeetCode 215 LeetCode 703 LeetCode 1046 LeetCode 378 LeetCode 347 #DSAinJavaScript #365daysOfCoding #JavaScriptDeveloper #LeetCodeSolutions #DataStructures #AlgorithmDesign #LogicBuilding #ProblemSolving #JavaScriptProgramming #TechLearning #HeapDataStructure #PriorityQueue #CodingInterview #SoftwareEngineering #KthLargest #Optimization #CleanCode #JavaScriptLogic #CareerGrowth #ProgrammingJourney
To view or add a comment, sign in
-
🚀 Day 18/50 – LeetCode Challenge 🧩 Problem #108 – Convert Sorted Array to Binary Search Tree Today’s problem focused on building a height-balanced Binary Search Tree (BST) from a sorted array — a great mix of recursion and tree concepts. 📌 Problem Summary: Given a sorted array, convert it into a height-balanced BST. A BST is height-balanced if the depth of the two subtrees of every node never differs by more than one. 🔍 Approach Used ✔ Used a divide and conquer strategy ✔ Selected the middle element of the array as the root ✔ Recursively built the left subtree from the left half ✔ Recursively built the right subtree from the right half This ensures the tree remains balanced. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(log n) (recursion stack) 💡 Key Learning ✔ Understanding tree construction from arrays ✔ Applying recursion effectively ✔ Importance of choosing the middle element for balance ✔ Strengthening concepts of BST and divide & conquer This problem helped reinforce how recursive thinking can simplify complex tree-building problems. Consistency is the key to mastery 🚀 🔗 Problem Link: https://lnkd.in/gymByPPA #50DaysOfLeetCode #LeetCode #DSA #BinarySearchTree #Recursion #ProblemSolving #CodingJourney #FutureAIEngineer #Consistency
To view or add a comment, sign in
-
-
Day 78/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 141 – Linked List Cycle (Easy) 🧠 Approach: Use Floyd’s Cycle Detection (Tortoise & Hare). Move one pointer one step and another two steps. If they meet, a cycle exists. 💻 Solution: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: slow = head fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: return True return False ⏱ Time | Space: O(n) | O(1) 📌 Key Takeaway: Using two pointers at different speeds is an efficient way to detect cycles without extra space. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Day 4 of 100 Days LeetCode Challenge. Problem: Special Positions in a Binary Matrix Today’s problem focused on matrix traversal + counting logic—simple concept, but requires careful observation. 💡 Key Insight: A position (i, j) is special if: mat[i][j] == 1 All other elements in the same row and column are 0 🔍 Efficient Approach: Count number of 1’s in each row Count number of 1’s in each column A position is special only if: Row count = 1 Column count = 1 👉 This avoids unnecessary repeated checks and improves efficiency. 🔥 What I Learned Today: Preprocessing (row & column counts) simplifies problems Avoid brute force → think in terms of frequency/counting Clean logic > complex code 📈 Challenge Progress: Day 4/100 ✅ Staying consistent! LeetCode, Matrix Problem, Arrays, Counting Technique, DSA Practice, Coding Challenge, Problem Solving, Algorithm Thinking, Optimization #100DaysOfCode #LeetCode #DSA #CodingChallenge #Matrix #ProblemSolving #TechJourney #ProgrammerLife #SoftwareDeveloper #CodingLife #LearnToCode #Developers #Consistency #GrowthMindset #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 180 of #200DaysOfCoding Today I solved “Special Positions in a Binary Matrix” on LeetCode. 🧠 Problem Idea: We are given a binary matrix and need to count positions where the value is 1 and all other elements in the same row and column are 0. Such positions are called special positions. 💡 Approach: Instead of checking the entire row and column every time, we can optimize by: • Counting the number of 1s in each row • Counting the number of 1s in each column • A cell (i, j) is special if: mat[i][j] == 1 rowCount[i] == 1 colCount[j] == 1 This reduces unnecessary checks and keeps the solution efficient. ⏱ Time Complexity: O(m × n) 📦 Space Complexity: O(m + n) 💻 Language Used: C++ Consistency is the real key. Every day of problem solving improves logical thinking and problem-solving ability. Just 20 more days to complete the #200DaysOfCoding challenge! 💪 #leetcode #codingchallenge #programming #cpp #datastructures #algorithms #softwaredevelopment
To view or add a comment, sign in
-
-
🚀 Day 135 – LeetCode 150 Days Challenge Today’s problem focused on the classic Coin Change Problem using Dynamic Programming (Top-Down / Memoization). 🧩 Problem Given an array of coin denominations and a target amount, find the minimum number of coins required to make that amount. If it is not possible, return -1. Example: Coins = [1,2,5], Amount = 11 Output → 3 (5 + 5 + 1) 💡 Key Idea This problem is similar to the Unbounded Knapsack pattern because we can use each coin multiple times. For each coin, we have two choices: 1️⃣ Not Take the coin → Move to the previous coin 2️⃣ Take the coin → Stay on the same coin index and reduce the amount We use recursion + memoization to store already computed states. State definition: dp[i][amount] Minimum coins required using coins from 0..i to make amount. ⚙️ Base Cases ✔ If amount == 0 → we need 0 coins ✔ If we are at the first coin (i == 0): If the amount is divisible by coins[0], return amount / coins[0] Otherwise return a large value (1e9) to represent impossible 🧠 Complexity ⏱ Time Complexity: O(N × Amount) 💾 Space Complexity: O(N × Amount) (DP table + recursion stack) 📌 What I Learned • How unbounded choices change DP transitions • The importance of handling impossible states using large values • How memoization reduces exponential recursion to polynomial time 🔥 Consistency > Motivation Day 135 completed, and the journey continues! #LeetCode #DSA #DynamicProgramming #CodingChallenge #150DaysOfCode #LeetCode150 #ProblemSolving
To view or add a comment, sign in
-
-
Pattern Recognition Mastery on Day 224 Today Today I focused on a very important skill in competitive programming which is pattern recognition. Understanding the keywords in a problem description can help you choose the right data structure immediately. When I see words like subarray or substring combined with a window I know it is a Sliding Window problem. If the question asks for the maximum or minimum value in that window I know I need to use a Deque to keep the solution efficient. I applied this logic to solve LeetCode 239 which is the Sliding Window Maximum problem. Instead of checking every window from scratch I used a monotonic decreasing deque. This allows me to keep track of the largest elements in linear time. The core idea is to remove indices from the back of the queue if the new number is larger than the old ones. This ensures that the largest value is always at the front of the queue. Once the window moves past an index I simply remove it from the front. Learning these rules helps me solve hard problems much faster. It is all about building a mental rule book for different types of array and string challenges. LeetCode question solved today: 215 Sliding Window Maximum #DSAinJavaScript #365daysOfCoding #JavaScriptLogic #LeetCode #SlidingWindow #Deque #PatternRecognition #AlgorithmDesign #ProblemSolving #DataStructures #LogicBuilding #CodingChallenge #SoftwareEngineering #ProgrammingDaily #WebDevelopment #TechLearning #JSAlgorithms #CleanCode #InterviewPrep #SoftwareDeveloper
To view or add a comment, sign in
-
🚀 Day 6 of 100 Days LeetCode Challenge Problem: Check if Binary String Has at Most One Segment of Ones Today’s problem is all about pattern observation in strings—simple, but easy to overthink. 💡 Key Insight: The string should contain only one continuous block of '1's. 👉 That means: Once a 0 appears after a 1, There should be no more '1's later 🔍 Simplest Trick: Just check if the pattern "01" appears more than once OR even better → check if "10" appears followed by another "1" 💡 Cleaner approach: Traverse the string Count transitions from 1 → 0 If you ever see 1 again after that → ❌ Invalid 🔥 What I Learned Today: Many problems are just pattern validation Clean logic beats complex conditions Always try to reduce the problem to a simple rule 📈 Challenge Progress: Day 6/100 ✅ Consistency building strong! LeetCode, Strings, Pattern Recognition, Greedy, DSA Practice, Coding Challenge, Problem Solving, Algorithm Thinking, Programming #100DaysOfCode #LeetCode #DSA #CodingChallenge #Strings #ProblemSolving #TechJourney #ProgrammerLife #SoftwareDeveloper #CodingLife #LearnToCode #Developers #Consistency #GrowthMindset #InterviewPrep
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development
Fibonacci series easy