zoukankan      html  css  js  c++  java
  • 155. Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

    • push(x) -- Push element x onto stack.
    • pop() -- Removes the element on top of the stack.
    • top() -- Get the top element.
    • getMin() -- Retrieve the minimum element in the stack.

    Example:

    MinStack minStack = new MinStack();
    minStack.push(-2);
    minStack.push(0);
    minStack.push(-3);
    minStack.getMin();   --> Returns -3.
    minStack.pop();
    minStack.top();      --> Returns 0.
    minStack.getMin();   --> Returns -2.
    

    存一个instance variable表示到目前为止的最小值
    stack里存的是:
         如果当前值大于之前的最小值,存当前值-最小值(正数)
         如果当前值小于之前的最小值,则这个值变成新的最小值,存当前值-最小值(负数)并更新最小值
    pop: pop出栈顶元素cur, 如果cur < 0,说明当前pop出的值是当前最小值,出栈后把最小值更新为min - cur; 如果cur > 0, 说明当前值大于当前最小值,应该是cur + pop
    top: 栈顶元素cur = stack.peek(), 如果cur < 0, 说明当前值是最小值,返回min,如果cur> 0, 说明当前值大于最小值,实际为cur + min
    public class MinStack {
        Stack<Long> stack;
        int min;
        /** initialize your data structure here. */
        public MinStack() {
            stack = new Stack<Long>();
            min = 0;
        }
        
        public void push(int x) {
            if (stack.isEmpty()) {
                min = x;
                stack.push(0L);
                return;
            }
            stack.push((long)x - min);
            if (x < min) {
                min = x;
            }
        }
        
        public void pop() {
            long cur = stack.pop();
            if (cur < 0) {
                min = (int)(min - cur);
            }
        }
        
        public int top() {
            if (stack.peek() < 0L) {
                return min;
            } else {
                return (int)(min + stack.peek());
            }
        }
        
        public int getMin() {
            return min;   
        }
    }
    
    /**
     * 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();
     */
    

     另一种办法

    分析:保存到目前为止的最小值。当出栈导致更新最小值时,如果记录了之前的最小值信息,就能更新到pop出当前元素后的最小值。

    public class MinStack {
        Stack<Integer> stack;
        int min;
        /** initialize your data structure here. */
        public MinStack() {
            stack = new Stack<Integer>();
            min = Integer.MAX_VALUE;
        }
        
        public void push(int x) {
            if (x <= min) { //这里要用<=。如果x == min,x也会被判断为新的最小值,在pop时连pop两次,因此此时也要push两次
                stack.push(min);
                min = x;
            }
            stack.push(x);
        }
        
        public void pop() {
            int cur = stack.pop();
            if (cur == min) {
                min = stack.pop();
            }
        }
        
        public int top() {
            return stack.peek();
        }
        
        public int getMin() {
            return min;
        }
    }
    
    /**
     * 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();
     */
    
  • 相关阅读:
    eclipse下c/cpp " undefined reference to " or "launch failed binary not found"问题
    blockdev 设置文件预读大小
    宝宝语录
    CentOS修改主机名(hostname)
    subprocess报No such file or directory
    用ldap方式访问AD域的的错误解释
    英特尔的VTd技术是什么?
    This virtual machine requires the VMware keyboard support driver which is not installed
    Linux内核的文件预读详细详解
    UNP总结 Chapter 26~29 线程、IP选项、原始套接字、数据链路访问
  • 原文地址:https://www.cnblogs.com/yuchenkit/p/7169507.html
Copyright © 2011-2022 走看看