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
LeetCode 70: Climbing Stairs with Recursion and DP
More Relevant Posts
-
🚀 Day 54 of LeetCode Problem Solving Journey — 100 Days LeetCode Challenge Today, I solved LeetCode #2390 — Removing Stars From a String using C++, under the guidance of Trainer NEKAL SINGH SALARIA Singh at REGex Software Services. 🔍 Problem Summary: You’re given a string containing letters and * characters. Each * removes: itself, and the closest non-star character to its left. The goal is to return the final string after all stars are processed. 🧠 Approach Idea: This problem maps perfectly to a stack simulation: Traverse characters one by one If character ≠ * → push to stack If character = * → pop last character from stack At the end, rebuild the result from the stack. 🧠 Key Learnings of the Day: ✔ Stack is ideal for “remove last element” operations ✔ Problems involving undo/backspace logic often use stack ✔ Simulation problems reward step-by-step thinking ✔ Pattern recognition helps solve similar problems faster ⏱️ Complexity Analysis: • Time Complexity: O(n) • Space Complexity: O(n) 💡 Many string problems are actually disguised stack problems — recognizing that pattern is a big win. #Day54 #100DaysLeetCodeChallenge #LeetCode #RegexSoftwareServices #NekalSingh #ProblemSolving #DSA #CPlusPlus #CodingChallenge #ProgrammingJourney #Stack #Strings #KeepGrowing
To view or add a comment, sign in
-
-
🔥 Day 10 of my coding consistency journey. Today I solved LeetCode Problem 1018 – Binary Prefix Divisible By 5. The task is to check whether the binary number formed at each prefix is divisible by 5. 💡 My Approach: • Instead of forming large binary numbers, I kept track of the current value modulo 5. • For each bit, I updated the value using: num = (num * 2 + current_bit) % 5 • If the result becomes 0, it means the current prefix is divisible by 5. • I stored the result for each prefix in a boolean array. This approach avoids overflow and keeps the solution efficient. Problems like this highlight the importance of modular arithmetic in optimization. Consistency is slowly turning into strength 🚀 #LeetCode #DSA #Algorithms #CodingJourney #ProblemSolving
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
-
-
LeetCode 90 — Subsets II Worked on generating all possible subsets of an array that may contain duplicates. This is an extension of the Subsets problem, but duplicates add a small twist. If handled carelessly, duplicate subsets get generated. Approach — Backtracking + Duplicate Skipping - Sorted the array first to group duplicates together - At each index, made the usual include / exclude decision - While skipping (not taking), moved the index forward past duplicate values - Ensured that identical elements don’t start identical branches Core idea :- Instead of filtering duplicates at the end, avoid creating them during recursion itself. Key learning: - Handling duplicates is usually about controlling the starting point of recursion branches. - Small adjustments in index movement can prevent large amounts of redundant work. This felt like a refined version of the basic subsets pattern. #leetcode #recursion #backtracking #dsa #algorithms #problemSolving #codingjourney
To view or add a comment, sign in
-
-
LeetCode 78 — Subsets Worked on generating all possible subsets (power set) of a given array of unique elements. Approach — Backtracking (Inclusion / Exclusion) - For every element, there are two choices: include it or exclude it - Used recursion to explore both branches at each index - Base case: when index reaches the array length → store the current subset - Maintained state using add → recurse → remove (backtracking step) - Total subsets formed = 2ⁿ Key learning: - This problem made the recursion tree very clear. - Every element doubles the number of possibilities. Once the inclusion/exclusion pattern is understood, many subset and combination problems follow the same structure. #leetcode #recursion #backtracking #dsa #algorithms #codingjourney #problemSolving
To view or add a comment, sign in
-
-
🚀 LeetCode 762 – Prime Number of Set Bits in Binary Representation 🧠 Difficulty: Easy 🧩 The Core Idea For every number in the range [left, right]: Count the number of set bits (1s) Check whether that count is a prime number That’s it. Simple in statement — sharp in execution. 💡 Key Observations Numbers are ≤ 10⁶ Maximum possible set bits for such numbers is small (≤ 20) So prime checking is extremely lightweight Using a built-in bit-counting function makes it super efficient ⚡ ⚡ Result ✅ Accepted ⚡ Runtime: 0 ms 💯 Beats 100% 🎯 Takeaway Even “Easy” problems can reinforce: Strong fundamentals in bit manipulation Efficient use of built-in tools Clean logical thinking Small wins compound. Consistency builds mastery. On to the next one. 🚀 #LeetCode #DSA #BitManipulation #LearningInPublic #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
#Day 6- Solved an Interesting Problem on LeetCode Today! Problem: Minimum Number of Seconds to Make Mountain Height Zero (LeetCode 3296) Today I worked on a challenging problem that involves optimizing the time required for multiple workers to reduce a mountain's height to zero. 🔍 Key Idea: The time required by each worker increases as they remove more height. So instead of simulating every second, we can use Binary Search on Time to efficiently determine the minimum number of seconds needed. 💡 Approach: • Use Binary Search on the possible time range • For a given time, calculate how much height each worker can remove • Sum up the heights removed by all workers • If the total is greater than or equal to the mountain height, the time is feasible 📈 Time Complexity: O(n log T) This problem was a great example of applying Binary Search on Answer, a powerful pattern in algorithmic problem solving. Always learning, always improving. 💻 #DSA #LeetCode #ProblemSolving #Algorithms #CodingJourney #C++
To view or add a comment, sign in
-
-
Day 72 | #100DaysOfCode | #100DaysOfLearning 🧿 Continuing the Binary Tree pattern ✅ Solved: Same Tree (Leetcode 100) Approach: • If both nodes are NULL → trees are same • If one is NULL → not same • If values are equal → recursively check left & right subtrees This problem helped me clearly understand how recursion compares two trees simultaneously. 🧠 What I learned: ✔️ Structural comparison of trees ✔️ Writing clean recursive base cases ✔️ Thinking in terms of subtree matching Trees are becoming more intuitive step by step Consistency continues. #DSA #BinaryTree #Recursion #TreeProblems #Leetcode #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
HI CONNECTIONS I recently tackled LeetCode 812, a problem that tests your ability to iterate through combinations and track global maximums. 🔍 The Challenge Given a set of (x, y) coordinates on a 2D plane, find the three points that, when connected, form a triangle with the largest possible area. 🛠️ My Approach: The "Exhaustive Search" Strategy Since the constraints for this problem are relatively small (up to 50 points), the most reliable way to solve it is to check every single possibility. Triple-Nested Iteration: I wrote a loop that picks three distinct points at a time from the list. The Comparison: For every triplet, I calculated the area formed by those three specific "corners." Tracking the Leader: I maintained a max_area variable. If a new triplet produced a larger area than the current leader, I updated the record. Final Result: Once every combination was checked, the largest area found was returned. 📊 Efficiency Time Complexity: O(n^3) — With 50 points, this results in about 125,000 operations, which a modern processor handles in milliseconds. Space Complexity: O(1) — We only need a few variables to track the current and maximum areas. 💡 Key Takeaway Not every problem requires a complex, optimized algorithm like Dynamic Programming. When the dataset is small, a Brute Force approach is often the best choice because it is easy to implement, easy to debug, and guaranteed to find the correct answer. #LeetCode #ProblemSolving #Algorithms #CodingLife #SoftwareEngineering #Geometry
To view or add a comment, sign in
-
-
🚀 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
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