Optimized MinStack 🔥| Constant Time Min Retrieval 🚀
IntuitionTo efficiently retrieve the minimum value in constant time, a single stack is used with an auxiliary technique:
Store previous min values: When pushing
# Intuition
To efficiently retrieve the minimum value in constant time, a single stack is used with an auxiliary technique:  
- **Store previous min values**: When pushing a new minimum, store the old min first.  
- **Pop min values correctly**: If the popped value is the current min, restore the previous min from the stack.  
- This avoids using an extra stack while ensuring **O(1) min retrieval**.

# Approach
1. **Push:**  
   - If `val` is less than or equal to `min`, store the current `min` before pushing `val`.
   - Update `min`.  
2. **Pop:**  
   - If popping the `min`, retrieve the previous min from the stack.  
3. **Top:**  
   - Return the top element.  
4. **GetMin:**  
   - Directly return `min`.

# Complexity
- **Time Complexity:** $$O(1)$$ for all operations  
- **Space Complexity:** $$O(n)$$ for stack storage  

# Code
```java []
class MinStack {
    int min = Integer.MAX_VALUE; 
    Stack<Integer> st = new Stack<>();

    public void push(int val) {
        if (val <= min) {
            st.push(min); // Store previous min before updating
            min = val;
        }
        st.push(val);
    }
    
    public void pop() {
        if (st.pop() == min) {
            min = st.pop(); // Restore previous min
        }
    }
    
    public int top() {
        return st.peek();
    }
    
    public int getMin() {
        return min;
    }
}

```
**Please Upvote⬆️**
![Screenshot 2025-02-15 134751.png](https://assets.leetcode.com/users/images/e2bb9e4d-8894-4f0e-9185-136ec2b5f34d_1740234280.6235588.png)

['Stack', 'Java']