从堆栈中查找最小值

时间:2016-09-09 15:28:05

标签: java data-structures stack

我试图从LeetCode解决这个Stack问题,我只能传递14/18个测试用例,因为测试不可用我无法弄清楚缺少哪些边缘情况。我是Java的新手,所以非常感谢: - )

          public class MinStack {

              private int top;
              private ArrayList<Integer> stack;
              private ArrayList<Integer> minStack;

              /** initialize your data structure here. */
              public MinStack() {
                  this.top = -1;
                  this.stack = new ArrayList<Integer>();
                  this.minStack = new ArrayList<Integer>();
              }

              public void push(int x) {
                  top++;
                  stack.add(x);
                  if(top == 0){
                      minStack.add(x);
                  }
                  else{
                      minStack.add(Math.min(minStack.get(top-1), x));

                  }
              }

              public void pop() {
                  stack.remove(stack.get(top));
                  minStack.remove(minStack.get(top));
                  top--;
              }

              public int top() {
                  if(top >= 0)
                      return stack.get(top); 
                  return -1;
              }

              public int getMin() {
                  if(top >= 0)
                      return minStack.get(top);
                  return -1;
              }
          }

          /**
           * Your MinStack object will be instantiated and called as such:
           * MinStack obj = new MinStack();
           * obj.push(x);
           * obj.pop();
           * int param_3 = obj.top();
           * int param_4 = obj.getMin();
           */

3 个答案:

答案 0 :(得分:0)

  1. 您可以将Stack<Integer>用于堆栈,将length属性用于堆栈顶部。

  2. 执行stack.remove(Object o)会删除支持数据结构中第一次出现的对象,即您的案例中的ArrayList。如果您使用Stack<Integer>,只需致电stack.pop()即可。或者,如果使用ArrayList,则调用stack.remove(top)

  3. 算法对我来说很好。

答案 1 :(得分:0)

我认为当堆栈中的顶部元素弹出并且用户要求堆栈中的当前最小值时会出现错误....

考虑一堆pop操作1,7,3,4,2,8,9毫无疑问,1这个堆栈的头部是最小的。但是如果调用pop,则删除1 ...由于没有正确的排序,不能保证堆栈顶部的下一个元素将是最小的。

在这一行

minStack.add(Math.min(minStack.get(top-1),x));

和上面的例子,1总是被添加到堆栈中,是最小的。因此,当弹出1时,1仍然是你的minStack中的最小值,但是上面的例子应该是2

答案 2 :(得分:0)

尝试此解决方案。

    // Java program to implement a stack that supports 
// getMinimum() in O(1) time and O(1) extra space. 
import java.util.*; 

// A user defined stack that supports getMin() in 
// addition to push() and pop() 
class MyStack 
{ 
    Stack<Integer> s; 
    Integer minEle; 

    // Constructor 
    MyStack() { s = new Stack<Integer>(); } 

    // Prints minimum element of MyStack 
    void getMin() 
    { 
        // Get the minimum number in the entire stack 
        if (s.isEmpty()) 
            System.out.println("Stack is empty"); 

        // variable minEle stores the minimum element 
        // in the stack. 
        else
            System.out.println("Minimum Element in the " + 
                               " stack is: " + minEle); 
    } 

    // prints top element of MyStack 
    void peek() 
    { 
        if (s.isEmpty()) 
        { 
            System.out.println("Stack is empty "); 
            return; 
        } 

        Integer t = s.peek(); // Top element. 

        System.out.print("Top Most Element is: "); 

        // If t < minEle means minEle stores 
        // value of t. 
        if (t < minEle) 
            System.out.println(minEle); 
        else
            System.out.println(t); 
    } 

    // Removes the top element from MyStack 
    void pop() 
    { 
        if (s.isEmpty()) 
        { 
            System.out.println("Stack is empty"); 
            return; 
        } 

        System.out.print("Top Most Element Removed: "); 
        Integer t = s.pop(); 

        // Minimum will change as the minimum element 
        // of the stack is being removed. 
        if (t < minEle) 
        { 
            System.out.println(minEle); 
            minEle = 2*minEle - t; 
        } 

        else
            System.out.println(t); 
    } 

    // Insert new number into MyStack 
    void push(Integer x) 
    { 
        if (s.isEmpty()) 
        { 
            minEle = x; 
            s.push(x); 
            System.out.println("Number Inserted: " + x); 
            return; 
        } 

        // If new number is less than original minEle 
        if (x < minEle) 
        { 
            s.push(2*x - minEle); 
            minEle = x; 
        } 

        else
            s.push(x); 

        System.out.println("Number Inserted: " + x); 
    } 
};