Design Min Stack with Constant Time Complexity

Day 25 of Daily DSA 🚀 Solved LeetCode 155: Min Stack ✅ Problem: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Approach: Used two stacks: main stack → stores all elements min stack → keeps track of the current minimum Key idea: When pushing a value, also push it to min stack if it is ≤ current minimum During pop, if the popped value equals the top of min stack, pop from min as well This ensures the minimum element is always available in O(1) time. ⏱ Complexity: • push() → O(1) • pop() → O(1) • top() → O(1) • getMin() → O(1) 📊 LeetCode Stats: • Runtime: 5 ms (Beats 83.96%) ⚡ • Memory: 47.15 MB Great problem to understand stack design patterns and auxiliary data structures. #DSA #LeetCode #Java #DataStructures #CodingJourney #ProblemSolving

  • text

To view or add a comment, sign in

Explore content categories