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

push(x) -- Push element x onto stack.
pop() -- Remove the element on top of the stack and return it.
top() -- Get the element on the top.
peekMax() -- Retrieve the maximum element in the stack.
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
Note:
-1e7 <= x <= 1e7
Number of operations won't exceed 10000.
The last four operations won't be called when stack is empty.

1. Solution: add popmax this func, you can pop out max at any time , so max stack to track the max elements  does not work as minstack(leetcode 155) did

add one more stack for helping

1. pop out the ele in the origin stack until find the max

2. push all into temp stack

3. use push(self built func) to push all the ele from temp stack into original stack

class MaxStack {
//becasuse of popMAx, two stacks is not enough
List<Integer> s1 ;//store ele
List<Integer> s2 ;//store min ele
/** initialize your data structure here. */
public MaxStack() {
s1 = new ArrayList<Integer>();
s2 = new ArrayList<Integer>();
} public void push(int x) {
s1.add(x);
if(s2.isEmpty() || s2.get(s2.size()-1) <= x) s2.add(x);
} public int pop() {
if(s1.isEmpty()) return 0;
int ele = s1.remove(s1.size()-1);
if(!s2.isEmpty() && ele == s2.get(s2.size()-1)){
s2.remove(s2.size()-1);
}
return ele;
} public int top() {
if(!s1.isEmpty())
return s1.get(s1.size()-1);
return 0;
} public int peekMax() {
if(!s2.isEmpty())
return s2.get(s2.size()-1);
return 0;
} //handful problem
public int popMax() {
//pop max
if(!s1.isEmpty() && !s2.isEmpty()){
int ele = s2.remove(s2.size() - 1);
List<Integer> s3 = new ArrayList<>();
while(ele != s1.get(s1.size() - 1)){
int temp = s1.remove(s1.size() - 1);
s3.add(temp);
}
s1.remove(s1.size() - 1);
while(!s3.isEmpty()){
push(s3.remove(s3.size()-1));
}
return ele;
}
return 0;
}
} /**
* Your MaxStack object will be instantiated and called as such:
* MaxStack obj = new MaxStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.peekMax();
* int param_5 = obj.popMax();
*/

2. solution 2: https://leetcode.com/problems/max-stack/discuss/153748/Java-LinkedList-and-PriorityQueue  (using linkedlist and priorityQueue)

Leave a comment u have a question!

716. Max Stack (follow up questions for min stack)的更多相关文章

  1. 带最小值操作的栈 · Min Stack

    [抄题]: 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. [思维问题]: [一句话思 ...

  2. [leetcode]716. Max Stack 最大栈

    Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...

  3. 155. Min Stack

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

  4. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  5. [CareerCup] 3.2 Min Stack 最小栈

    3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...

  6. leetcode 155. Min Stack --------- java

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

  7. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

  8. Min Stack

    Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...

  9. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

随机推荐

  1. Zabbix触发器函数之count函数

    一.背景 zabbix监控中我们用的最多的是count这个函数,通过确认多次可以减少很多误告警,提高了运维效率.可以设置连续几次都异常才发出告警,这样一来,只要发出告警基本上就已经确定发生故障了. 二 ...

  2. Java中使用nextLine(); 没有输入就自动跳过的问题

    转自:https://www.cnblogs.com/1020182600HENG/p/6564795.html [问题分析] 必要的知识:in.nextLine();不能放在in.nextInt() ...

  3. $bzoj1007-HAOI2008$ 水平可见直线 下凸包

    题面描述 在\(xOy\)直角坐标平面上有\(n\)条直线\(L_1,L_2,...,L_n\),若在\(y\)值为正无穷大处往下看,能见到\(L_i\)的某个子线段,则称\(L_i\)为可见的,否则 ...

  4. Android报错

      Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process. ...

  5. 抽象工厂方法模式(Abstract Factory Pattern)

    Provide an interface for creating families of related or dependent objects without specifying their ...

  6. vim创建新的命令

    转自:http://man.chinaunix.net/newsoft/vi/doc/usr_5F40.html#usr_40.txt *40.1* 键映射 简单的映射已经在 |05.3| 介绍过了. ...

  7. js消息提示框插件-----toastr用法

     (本文系转载) 因为个人项目中有一个提交表单成功弹出框的需求,从网上找了一些资料,发现toastr这个插件的样式还是不错的.所以也给大家推荐下,但是网上的使用资料不是很详细,所以整理了一下,希望能给 ...

  8. 适用于所有页面的基础样式base.css

    @charset "UTF-8"; /*css 初始化 */ html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, ...

  9. MAC 下安装RabbitMQ

    1.使用brew来安装 RabbitMQ(地址:http://www.rabbitmq.com/install-standalone-mac.html ) 2.安装目录 /usr/local/Cell ...

  10. Spring mvc框架下使用kaptcha生成验证码

    1.下载jar包并导入. kaptcha-2.3.2.jar 2.spring 配置文件 applicationContext.xml. <bean id="captchaProduc ...