Day 48 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Next Greater Element We were given an array. For each element, we had to find the next greater element on the right side. If none exists → return -1. Example: [4, 5, 2, 10] → [5, 10, 10, -1] 💻 Brute Force Approach 🔹️For each element, check all elements on the right. 🔹️Find the first greater element. 🔹️If found → store it. 🔹️Else → store -1. Time Complexity: O(n²) Space Complexity: O(1) Works. But slow. 💻 Optimal Approach (Using Stack) 🔹️Traverse from right to left. 🔹️Use a stack to store elements. 🔹️For each element: ▪️Pop smaller elements from stack ▪️Top of stack = next greater element ▪️If stack empty → answer is -1 🔹️Push current element into stack Efficient. Single pass. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 📚 What I learned today: ▫️Monotonic stack helps in nearest greater problems. ▫️Traversing from right simplifies logic. ▫️Popping smaller elements keeps stack useful. ▫️Stack stores only potential answers. Day 48 completed. Stack patterns getting stronger 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
Next Greater Element Challenge Solved with Stack
More Relevant Posts
-
The Day's Particulars: #Day15 of the LeetCode Challenge 💠 Problems Conquered: Palindrome Linked List & Remove Nodes From Linked List 💠 Patterns Explored: Code Reusability, The DRY Principle, Linked List Reversal 💠 Future Notes to Self: A well-written helper method is worth its weight in gold. Writing clean, modular code means you can solve multiple complex problems with the exact same foundational logic. 𝐃𝐞𝐚𝐫𝐞𝐬𝐭 𝐆𝐞𝐧𝐭𝐥𝐞 𝐑𝐞𝐚𝐝𝐞𝐫𝐬, We have officially reached the halfway mark of this 30-day journey! Day 15 was less about learning a brand new algorithm and more about practical software engineering: the power of code reusability. Today's agenda featured two distinct challenges: 𝘗𝘢𝘭𝘪𝘯𝘥𝘳𝘰𝘮𝘦 𝘓𝘪𝘯𝘬𝘦𝘥 𝘓𝘪𝘴𝘵 𝘢𝘯𝘥 𝘙𝘦𝘮𝘰𝘷𝘦 𝘕𝘰𝘥𝘦𝘴 𝘍𝘳𝘰𝘮 𝘓𝘪𝘯𝘬𝘦𝘥 𝘓𝘪𝘴𝘵. For the Palindrome problem, the most optimal O(1) space approach is to find the middle of the list using slow and fast pointers, reverse the second half, and then compare the two halves. For the Remove Nodes problem, where you must delete any node that has a greater value to its right, the logic becomes trivial if you process the list backward. Since we only have forward pointers in a singly linked list, the solution was to reverse the entire list, iterate through to keep track of the maximum value (dropping any smaller nodes), and then reverse it back to its original state. The highlight of my day was not just securing the green "Accepted" checkmarks, but realizing that the exact same reverse() helper method was the MVP for both problems. Instead of rewriting logic, I simply extracted the list-reversal code into its own method and called it whenever needed. It was a great practical reminder of the DRY (Don't Repeat Yourself) principle. Build strong, modular pieces, and you can assemble them to solve almost anything. Halfway there, and the momentum is going strong! Yours in code, Saumya #LeetCode #DailyCoding #LinkedList #CodeReusability #JavaDeveloper #LadyWhistledown #SoftwareEngineering #dsa_stories #30daysOfCode #TechCommunity #DRYPrinciple #DataStructures #Algorithms
To view or add a comment, sign in
-
-
Day 49 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Remove All Adjacent Duplicates in String We were given a string s. Task was to remove all adjacent duplicates. If duplicates are removed, new duplicates may form. So we repeat until no duplicates remain. Example: "abbaca" → "ca" 💻 Approach (Using Stack) 🔹️Create an empty stack. 🔹️Traverse each character. 🔹️If stack is not empty and top == current character → pop 🔹️Else → push current character 🔹️At the end, build string from stack Duplicates get removed automatically. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 📚 What I learned today: ▫️Stack helps in handling adjacent comparisons. ▫️Removing elements can create new patterns. ▫️Single pass solution is possible with stack. ▫️String problems often map to stack operations. Day 49 completed. Almost at 50 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 94 of #365DaysOfLeetCode Challenge Today’s focus: **Mastering Greedy + Stack Patterns** After solving problems like *Remove K Digits*, one thing is becoming clear — a lot of “hard-looking” problems become easier when you recognize the **pattern behind them**. 💡 **Today’s Learning:** Instead of jumping into code, I spent time identifying: * When to use **Greedy** * When a **Stack** can simplify decisions * How to think in terms of **local vs global optimum** 📌 **Key Insight:** Many problems follow this idea: 👉 *“If the current choice is better than the previous one, fix it immediately.”* This applies to: * Removing digits (monotonic stack) * Stock span problems * Next greater/smaller elements * Histogram problems ⚡ **Mindset Upgrade:** Earlier → “Try all possibilities” Now → “What is the best decision I can make right now?” **What I learned today:** Data Structures are not just tools — they are **decision-making frameworks**. Day 94 done The journey is slowly shifting from *solving problems* → to *thinking like an engineer*. #LeetCode #DSA #CodingChallenge #Greedy #Stack #ProblemSolving #TechJourney #Consistency
To view or add a comment, sign in
-
-
The Day's Particulars: #Day23 of the 30 Days of Code Challenge 💠Problems Conquered: Reverse Nodes in k-Group, Swap Nodes in Pairs, Linked List Random Node, Merge In Between Linked Lists, and Merge Nodes in Between Zeros 💠Patterns Explored: Segmented Reversal, Conditional Node Merging, and Random Selection 💠Future Notes to Self: Solving a complex problem becomes significantly less intimidating when you realize it is often just a generalized version of a smaller, easier problem. Master the base case, and the complex case naturally follows! 𝐃𝐞𝐚𝐫𝐞𝐬𝐭 𝐆𝐞𝐧𝐭𝐥𝐞 𝐑𝐞𝐚𝐝𝐞𝐫𝐬, Day 23 was an absolute marathon! I managed to knock out five distinct Linked List problems today, heavily focusing on segmented pointer manipulation. The highlight of the day was tackling 𝘚𝘸𝘢𝘱 𝘕𝘰𝘥𝘦𝘴 𝘪𝘯 𝘗𝘢𝘪𝘳𝘴 alongside my second Hard problem: 𝘙𝘦𝘷𝘦𝘳𝘴𝘦 𝘕𝘰𝘥𝘦𝘴 𝘪𝘯 𝘬-𝘎𝘳𝘰𝘶𝘱. I quickly realized that swapping nodes in pairs is essentially just reversing nodes in a k-group where k = 2. By writing a solid helper function to reverse a specific segment of a list, I could cleanly apply the exact same underlying logic to solve both problems. It was a great lesson in how complex problems are just simpler problems scaled up. I also worked through 𝘔𝘦𝘳𝘨𝘦 𝘐𝘯 𝘉𝘦𝘵𝘸𝘦𝘦𝘯 𝘓𝘪𝘯𝘬𝘦𝘥 𝘓𝘪𝘴𝘵𝘴 and 𝘔𝘦𝘳𝘨𝘦 𝘕𝘰𝘥𝘦𝘴 𝘪𝘯 𝘉𝘦𝘵𝘸𝘦𝘦𝘯 𝘡𝘦𝘳𝘰𝘴. Both were fantastic exercises in conditional pointer reassignment, carefully traversing to specific boundaries (either indices or specific node values like zero) and cleanly snipping out or replacing entire chunks of the list without losing the rest of the chain. Finally, 𝘓𝘪𝘯𝘬𝘦𝘥 𝘓𝘪𝘴𝘵 𝘙𝘢𝘯𝘥𝘰𝘮 𝘕𝘰𝘥𝘦. offered a completely different flavor. It required fetching a random node with equal probability, which shifts the focus away from pure structural manipulation and into mathematical probability and data retrieval. The momentum is real, and the pointer logic is feeling like second nature now. On to Day 24! Yours in code, Saumya #LeetCode #30DaysOfCode #JavaDeveloper #BackendDeveloper #DataStructures #Algorithms #ProblemSolving #TechJourney #LinkedLists #TwoPointers #PointerManipulation #SoftwareEngineering #TechCommunity #DailyCoding #GrowthMindset #dsaStories
To view or add a comment, sign in
-
-
Day 30 – LeetCode Journey 🚀 Solved Trapping Rain Water — a classic Hard problem that tests deep understanding of two-pointer optimization and space efficiency. Instead of brute force or extra space, applied an optimized two-pointer approach to compute trapped water in a single pass. 🔹 Time Complexity: O(n) 🔹 Runtime: 0 ms (Beats 100%) ⚡ 🔹 Memory Usage: Optimized This problem reinforced how powerful pointer techniques can be when combined with the right intuition. 30 days of consistency, learning, and growth — and just getting started 💪 #Day30 #LeetCode #DSA #ProblemSolving #CodingJourney #SoftwareEngineering #Consistency #KeepLearning
To view or add a comment, sign in
-
-
Day 6 of #100Days 💻 Solved today’s LeetCode Daily — Minimum Operations to Make a Uni-Value Grid 💡 Intuition (simple): We can only add/subtract a fixed value x, so all numbers must be compatible (difference divisible by x). If not → impossible. Then, flatten + sort the grid. To minimize operations, convert everything to the median — it gives the least total moves (classic trick!). ⚡ Complexity: Time: O(n log n) Space: O(n) 📌 Takeaway: Whenever you see “minimize total distance / operations” → think median #LeetCode #DSA #CodingInterview #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Just crossed 100 problems solved on LeetCode. Not impressive by itself—but what matters is consistency. Most of these came from sticking to fundamentals like arrays, two pointers, and basic problem patterns instead of jumping around randomly. Current focus: • Strengthening problem-solving patterns • Reducing time per problem • Moving from Easy → Medium consistently Still a long way to go. Next target: 200 with stronger Medium coverage. If you're grinding LeetCode too, focus less on quantity and more on pattern recognition—it compounds faster. #LeetCode #DSA #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 53 of #100DaysOfCode — Mastering In-Place Problem Solving Today’s focus: LeetCode #48 — Rotate Image🧠 At first glance, this problem looks simple… until you hit the constraint: 👉 You must rotate the matrix in-place (no extra space allowed). That’s where the real learning begins. 💡 What I learned today: * Difference between brute force vs optimized approach * Why space complexity matters in real-world systems * How to break a problem into steps: 1. Transpose the matrix 2. Reverse each row * Improved understanding of 2D array manipulation 🔍 Key Insight: Most problems aren’t about coding — they’re about *thinking in transformations. Once you visualize the pattern, the solution becomes elegant. 💻 Tech I’m sharpening daily: * Data Structures & Algorithms (DSA) * Problem-solving mindset * Writing clean & optimized C++ code 📈 Consistency Update: Even on days when it feels tough or slow, I’m showing up. Because growth isn’t about speed — it’s about discipline. 🎯 Goal: Become a strong problem solver ready for real-world engineering challenges. If you're also on a coding journey, let’s connect and grow together 🤝 #Day53 #100DaysOfCode #DSA #LeetCode #CodingJourney #SoftwareEngineering #ProblemSolving #CPlusPlus #TechCareers #LearningInPublic #Consistency #FutureEngineer
To view or add a comment, sign in
-
-
46 of #100DaysOfCode 🚀 Solved a classic stack problem today: removing adjacent duplicates from a string using a clean and efficient approach. 🔍 Key Idea: Use a stack to track characters — if the current character matches the top, pop it (duplicate removed). Otherwise, push it. Simple yet powerful! 💡 Learned: Stack isn’t just a data structure, it’s a mindset for handling problems involving reversals, matching, and cancellations. ⚡ Optimized further by using a string as a stack for better performance. #LeetCode #DataStructures #Stack #CPP #CodingJourney #KeepLearning
To view or add a comment, sign in
-
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