485 ms solution in C#


  • 0
    D
    public class MinStack 
    {
        private Stack<KeyValuePair<int, int>> stack = new Stack<KeyValuePair<int, int>>();
        
        public void Push(int x) 
        {
            int min = int.MinValue;
            if (stack.Count > 0) min = stack.Peek().Value;
            if (stack.Count == 0 || x < min) 
            {
                stack.Push(new KeyValuePair<int, int>(x, x));
            }
            else
            {
                stack.Push(new KeyValuePair<int, int>(x, min));
            }
        }
    
        public void Pop() 
        {
            stack.Pop();
        }
    
        public int Top() 
        {
            return stack.Peek().Key;
        }
    
        public int GetMin() 
        {
            return stack.Peek().Value;
        }
    }

  • 0
    C

    You didn't specify the case that when pop() an empty stack? for the java code, I need to check if the stack, or in my case, the linkedList is empty or not...

    Also, if the min to be the minimum integer, it won't be updated in the push...

    How can this pass the test?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.