zoukankan      html  css  js  c++  java
  • 716. Max Stack实现一个最大stack

    [抄题]:

    Design a max stack that supports push, pop, top, peekMax and popMax.

    1. push(x) -- Push element x onto stack.
    2. pop() -- Remove the element on top of the stack and return it.
    3. top() -- Get the element on the top.
    4. peekMax() -- Retrieve the maximum element in the stack.
    5. popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.

     

    Example 1:

    MaxStack stack = new MaxStack();
    stack.push(5); 
    stack.push(1);
    stack.push(5);
    stack.top(); -> 5
    stack.popMax(); -> 5
    stack.top(); -> 1
    stack.peekMax(); -> 5
    stack.pop(); -> 1
    stack.top(); -> 5

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    知道是用maxstack,但是写不出来

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    popmax的暂存值,还要再放回去,不能调用自己,所以再写一个pushHelper函数

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    stack maxstack必须要保持相同的长度,功能就是维持最大值而已

    [二刷]:

    x不是最大值,就pop到temp中去。此时maxStack也必须要同时pop,才能保持长度时时刻刻一致

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    保持长度时时刻刻一致

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    新建的stack,在主函数中直接用名字调用即可

    Stack<Integer> stack = new Stack<Integer>();
        Stack<Integer> maxStack = new Stack<Integer>();
        
        public MaxStack() {
            this.stack = stack;
            this.maxStack = maxStack;
        }

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

    就是新建一个类,然后调用方法就行了

    // package whatever; // don’t place package name!
    
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    class MaxStack {
    
        Stack<Integer> stack = new Stack<Integer>();
        Stack<Integer> maxStack = new Stack<Integer>();
        
        public MaxStack() {
            this.stack = stack;
            this.maxStack = maxStack;
        }
        
        public void pushHelper(int x) {
            //if the max exits
            int max = maxStack.isEmpty() ? Integer.MIN_VALUE : maxStack.peek();
            
            //push into 2 stacks
            //equal max or not
            if (x > max) {
                stack.push(x);
                maxStack.push(x);
            }else {
                stack.push(x);
                maxStack.push(max);
            }
        }
        
        public void push(int x) {
            pushHelper(x);
        }
    
        
        public int pop() {
            //pop the normal
            maxStack.pop();
            return stack.pop();
        }
        
        public int top() {
            //return the normal
            return stack.peek();
        }
        
        public int peekMax() {
            return maxStack.peek();
        }
        
        public int popMax() {
            //get max
            int max = maxStack.peek();
            
            //initialize a temp stack
            Stack<Integer> temp = new Stack<Integer>();
            
            //while x < max, store into temp
            while (stack.peek() < max) {
                int x = stack.pop();
                temp.push(x);
                //should pop at the same time
                maxStack.pop();
            }
            
            //once equal, pop both
            stack.pop();
            maxStack.pop();
            
            //retrive by pushHelper
            while (!temp.isEmpty()) {
                int x = temp.pop();
                pushHelper(x);
            }
            
            //return
            return max;
        }
    }
    
    
    class MyCode {
      public static void main (String[] args) {
        MaxStack answer = new MaxStack();
        answer.push(5);
        answer.push(10000);
        answer.push(4);
        answer.push(100);
        int rst = answer.popMax();
        System.out.println("rst = " +rst);
      }
    }
    View Code

     [潜台词] :

  • 相关阅读:
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    软件工程实践总结
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9380761.html
Copyright © 2011-2022 走看看