Solving LeetCode 155 - Min Stack with Constant Time

🚀 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

  • text

To view or add a comment, sign in

Explore content categories