C# implementation!


  • 0
    D
    Stack stack = new Stack();
         SortedDictionary<int, int> d = new SortedDictionary<int, int>();
         int min = 0;
        public  void Push(int x)
        {
            stack.Push(x);
            if (d.ContainsKey(x) == false)
            {
                d.Add(x, 1);
            }
            else if (d.ContainsKey(x) == true)
            {
                int count = d[x];
                d[x] = ++count;
            }
        }
    
        public  void Pop()
        {
            int i = Convert.ToInt32(stack.Pop());
            if(d[i]==1){
                d.Remove(i);
            }
            else
            {
                int count = d[i];
                d[i] = --count;
            }
        }
    
        public  int Top()
        {
           return Convert.ToInt32(stack.Peek());
        }
    
        public  int GetMin()
        {
            if (d == null)
            {
                return -1;
            }
            else
            {
                return d.Keys.First();
            }
        }

Log in to reply
 

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