Solved LeetCode 155 Min Stack with clever encoding

🚀 Day 66 of #100DaysOfCode Today’s problem was LeetCode 155 — Min Stack 🧠💪 📌 Problem Summary: We need to design a stack that supports: push() pop() top() getMin() (returns minimum element in O(1) time) All operations must be O(1) — that’s the tricky part. ⚡ 📌 Approach: 👉 Use a single stack to store modified values 👉 Track the minimum separately using a variable 👉 When pushing a smaller value, encode it cleverly so you can retrieve the previous min during pop() 💡 Key Insight: When inserting a value smaller than the current minimum: st.push(2*x - min) min = x This way, the stack encodes both data and min info efficiently. 📌 Complexity: ⏱ Time: O(1) for all operations 💾 Space: O(n) ✨ Learning: This is a great example of space optimization and bit manipulation logic — classic low-level trick that feels like magic the first time you see it! ⚙️ Raj Vikramaditya Raghav Garg Nancy Solanki Shweta Arora Harsh Raj Harshita Verma Love Babbar Prince Singh Shivam Mahajan Rohit Negi Neeraj Walia Nishant Chahar Kushal Vijay #LeetCode #100DaysOfCode #CodingChallenge #Cplusplus #DataStructures #LinkedList #ProblemSolving #SoftwareEngineering #Developers #Programming #TechJourney #KeepLearning #DailyCoding #CodeNewbie#LeetCode #100DaysOfCode #Programming #Cplusplus #LinkedList #ProblemSolving #SoftwareEngineering #TechJourney #CodeNewbie #DSA

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories