🚀 Day 36 of #100DaysOfCode Today I solved LeetCode 155 – Min Stack 💻 🔍 Problem Summary: Design a stack that supports push, pop, top, and retrieving the minimum element — all in constant time O(1). 🧠 Key Learning: This problem teaches how to optimize operations using an auxiliary stack to track the minimum element efficiently. 💡 Approach: Use two stacks: Main stack → stores all elements Min stack → keeps track of minimum values While pushing: Push element to main stack Push to min stack only if it’s smaller or equal While popping: Remove from min stack if it matches top of main stack 📊 Complexity: Time: O(1) for all operations Space: O(n) 🔥 Takeaway: Using extra space smartly can drastically improve performance. This is a great example of designing efficient data structures. #LeetCode #Java #Stack #DataStructures #Algorithms #CodingJourney #100DaysOfCode
Solving LeetCode 155 - Min Stack with Constant Time
More Relevant Posts
-
🚀 Solved LeetCode 2515 – Shortest Distance to Target String in a Circular Array Today I tackled an interesting problem that highlights the importance of handling circular data structures efficiently. 🔍 Problem Insight: Given a circular array of words, a target string, and a starting index, the goal is to find the minimum number of steps required to reach the target by moving either left or right. 💡 Key Learnings: Circular arrays require thinking beyond linear traversal Always consider both directions (forward & backward) Optimizing distance using min(distance, n - distance) is the key trick Simple logic + correct observation = optimal solution ⚙️ Approach Used: Traverse the array to find all occurrences of the target Calculate distance from the starting index Take the minimum considering circular movement 📈 Complexity: Efficient O(n) solution with constant space 🔥 Takeaway: This problem reinforced how small tweaks (like circular behavior) can change the entire approach. A great example of combining logic + observation for clean and optimal solutions. #LeetCode #Algorithms #ProblemSolving #CodingJourney #Java #DataStructures #CompetitiveProgramming
To view or add a comment, sign in
-
-
🚀 Day 39 of My LeetCode Journey 📌 Solved: LeetCode 1848 – Minimum Distance to the Target Element Today’s problem was simple yet a great exercise in improving logical thinking and iteration skills. 🔍 Problem Insight: Given an array, a target value, and a starting index, the goal is to find the minimum distance between the start index and any index where the target exists. 💡 Approach: Traverse the array Check for elements equal to the target Compute distance using |i - start| Keep track of the minimum value ⚡ Key Learning: Sometimes, the most optimal solution is a straightforward linear scan. Don’t overcomplicate when a simple approach works efficiently! ⏱ Complexity: Time: O(n) Space: O(1) 🔥 Consistency is key! Every day, one step closer to mastering Data Structures & Algorithms. #Day39 #LeetCode #Java #DSA #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 23 of my #30DayCodeChallenge: The Art of Pruning! The Problem: Permutations II. Generating all unique permutations of a collection that contains duplicates. The challenge isn't just finding the arrangements, but ensuring we don't repeat work or results. The Logic: This problem is a deep dive into Backtracking with a strategic Pruning layer: 1. The Frequency/State Tracking: Since we have duplicate numbers, we can't just rely on the values themselves. I used a vis [] (visited) boolean array to keep track of which specific index in the array is currently being used in our recursion tree. 2. Sorting for Symmetry Breaking: Before starting the recursion, sorting the array is the secret sauce. By grouping identical numbers together, we can easily identify when we are about to make a "dur"-ate choice." 3. Backtracking: Standard push, recurse, and pop. We explore every valid path, then "undo" our choice by setting vis [j] = false to backtrack and try the next possibility. One step closer to mastery. The logic is getting sharper! Onward to Day 24! #Java #Algorithms #DataStructures #Backtracking #LeetCode #150DaysOfCode #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 100 Days of Code Day-25 LeetCode Problem Solved: Reverse Nodes in k-Group I recently worked on a Linked List problem that focuses on reversing nodes in groups of size k while preserving the remaining structure if the group size is less than k. 🔹 Strengthened my understanding of pointer manipulation 🔹 Improved problem decomposition skills 🔹 Practiced recursive thinking for efficient implementation 💡 Key takeaway: Breaking complex problems into smaller, manageable parts significantly simplifies the solution approach. Continuing to build consistency in problem-solving and deepen my understanding of Data Structures & Algorithms. #LeetCode #DataStructures #Algorithms #Java #ProblemSolving #SoftwareDevelopment
To view or add a comment, sign in
-
-
Day 38 of #75DaysofLeetCode 🚀 Cracked LeetCode 236 – Lowest Common Ancestor of a Binary Tree! Understanding trees becomes powerful when you master problems like LCA 🌳 💡 Problem Insight: Given a binary tree, find the lowest node that has both p and q as descendants. 🔍 Key Idea: Instead of storing paths, we can solve this using a simple recursive DFS: If current node is null, p, or q → return it Search left and right subtree If both sides return non-null → 🎯 current node is the LCA Otherwise → return the non-null side ⚡ Why this works? Because the first node where both p and q appear in different subtrees is their lowest common ancestor. ⏱ Complexity: Time: O(N) Space: O(H) 📌 Takeaway: You don’t always need extra data structures—smart recursion can simplify complex tree problems! #LeetCode #DataStructures #Java #CodingInterview #BinaryTree #DFS #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 547 of #750DaysOfCode 🚀 🔥 Solved: Check if Strings Can be Made Equal With Operations II (LeetCode Medium) 💡 Problem Insight We can swap characters at indices i and j such that: 👉 (j - i) is even 🧠 Key Observation If (j - i) is even ⇒ 👉 indices belong to the same parity group So: Even indices form one group Odd indices form another group 👉 We can rearrange freely within each group 🚫 But cannot mix between them ⚡ Optimized Approach (Single Array Trick) Instead of using two arrays, we can: Use a single frequency array of size 52 First 26 → even indices Next 26 → odd indices 👉 Clever use of: int off = (i & 1) * 26; i & 1 = 0 → even → offset 0 i & 1 = 1 → odd → offset 26 🚀 Day 547 of #750DaysOfCode 🔥 Solved: Check if Strings Can be Made Equal With Operations II (LeetCode Medium) 💡 Problem Insight We can swap characters at indices i and j such that: 👉 (j - i) is even 🧠 Key Observation If (j - i) is even ⇒ 👉 indices belong to the same parity group So: Even indices form one group Odd indices form another group 👉 We can rearrange freely within each group 🚫 But cannot mix between them ⚡ Optimized Approach (Single Array Trick) Instead of using two arrays, we can: Use a single frequency array of size 52 First 26 → even indices Next 26 → odd indices 👉 Clever use of: int off = (i & 1) * 26; i & 1 = 0 → even → offset 0 i & 1 = 1 → odd → offset 26 📈 Complexity Time: O(n) Space: O(1) 💬 Key Takeaway This problem is a great example of: 👉 Index grouping + frequency balancing And the optimization shows: 👉 How bit manipulation (i & 1) + offset trick can reduce space & simplify logic 🔥 🔁 Small optimizations → Big impact over time Consistency continues 💯 #LeetCode #Algorithms #DataStructures #Java #ProblemSolving #CodingChallenge #750DaysOfCode #Consistency
To view or add a comment, sign in
-
-
🚀 Day 90 of My 200-Day DSA Challenge on LeetCode 🚀 📌 Focus Area: Hashing + Index Optimization Today I worked on a problem where we had to find the minimum distance between three equal elements in an array. At first, it feels like a brute-force problem (checking all triplets), but that would be too slow. The key was to optimize using indexing and mathematical observation. 🧠 Approach 🔹 Grouped indices of each number using hashing 🔹 Focused only on elements appearing at least 3 times 🔹 Observed an important pattern: 👉 Distance depends only on the first and last index of a triplet 🔹 Iterated through consecutive triplets of indices 🔹 Calculated distance efficiently using: 2 × (last − first) 📚 Key Takeaways 🔹 Mathematical simplification can eliminate unnecessary computations 🔹 Hashing helps reduce time complexity drastically 🔹 Instead of brute force, look for patterns in formulas 🔹 Consecutive grouping often leads to optimal solutions 📈 Progress Highlights ✅ Improved problem-solving using observation-based optimization ✅ Strengthened concepts of hashing and array indexing ✅ Learned how to convert a brute-force approach into an efficient solution #Day90 #200DaysOfCode #DSAChallenge #Algorithms #LeetCode #Java #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 27 – #100DaysOfLeetCode Challenge Today I solved the problem “Best Time to Buy and Sell Stock II.” Problem Summary: You are given an array prices where prices[i] is the price of a stock on day i. The goal is to maximize profit by buying and selling the stock multiple times. However, you must sell before you buy again. Approach – Greedy Strategy Instead of trying every possible transaction combination, the idea is simple: 🔹 If the next day's price is higher than today's price, we take the profit 🔹 Add all positive price differences 🔹 This captures every profitable opportunity in the price trend For example: If prices = [7,1,5,3,6,4] Profits = (5-1) + (6-3) = 7 Time Complexity: O(n) Space Complexity: O(1) Key Learning: A Greedy approach works well when we want to capture every incremental profit opportunity instead of trying complex combinations. Continuing my journey of strengthening Data Structures & Algorithms skills through the #100DaysOfLeetCode challenge. On to the next problem! #100DaysOfLeetCode #LeetCode #DSA #GreedyAlgorithm #ProblemSolving #Java #CodingJourney #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 563 of #750DaysOfCode 🚀 📌 Problem Solved: Shortest Distance to Target String in a Circular Array Today I explored a clean and optimized approach to solving a circular array problem 🔄 💡 Key Insight: Instead of checking all indices, we can expand from the start index in both directions simultaneously 👉 At each step i, we check: Forward → (start + i) % n Backward → (start - i + n) % n ⏱️ The moment we find the target, we return i → which is guaranteed to be the minimum distance 🧠 Why this works: We are exploring layer by layer (like BFS on array) First match = shortest path ✅ No need to scan entire array unnecessarily 🔥 What I Learned: Circular problems can often be solved using modulo arithmetic Expanding outward is more efficient than brute force Think in terms of minimum steps, not positions Consistency is the real game changer 💯 On to Day 564 🚀 #LeetCode #Java #Algorithms #DataStructures #CodingJourney #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
#100DaysOfCode | Day 7 of my LeetCode challenge : Arrays to Linked Lists! Problem - LeetCode #21 (Merge Two Sorted Lists). While this problem can be solved recursively, I chose the Two-Pointer Iterative Approach. Here is why: Production Safety: Recursion is elegant, but on massive datasets, it risks a StackOverflowError. Iteration is much safer for enterprise-level data. The "Dummy Node" Pattern: By initializing a sentinel/dummy node at the start, I eliminated the need to write clunky if-else checks to determine the new head. It keeps the code clean and implicitly handles edge cases like empty lists. The Complexity : ⏱️ Time Complexity: O(N + M) (Where N and M are the lengths of the two lists). 💾 Space Complexity: O(1) (No extra memory used, just re-pointing existing nodes). Getting this to run perfectly in my local IDE tonight was a great feeling. #100DaysOfCode #Java #LeetCode #SoftwareEngineering #SeniorEngineer #BankingTech #DataStructures #Algorithms
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