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. 剑指offer——面试题14:剪绳子

    // 面试题14:剪绳子 // 题目:给你一根长度为n绳子,请把绳子剪成m段(m.n都是整数,n>1并且m≥1). // 每段的绳子的长度记为k[0].k[1].…….k[m].k[0]*k[1 ...

  2. git——合并分支

    A将自己的本地代码提交到远程A分支,此时master主干上有B新提交的代码,如果此时A把自己的代码merge到主干,会有冲突,那怎么办? 1.A将自己的代码提交到自己的A分支 2.git fetch ...

  3. Java调度线程池ScheduleExecutorService

    如果在一个ScheduleExecutorService中提交一个任务,这个任务的调度周期设置 的时间比任务本身执行的时间短的话会出现什么情况?也就是在线程调度时间已经到了 但是上次的任务还没有做完的 ...

  4. shell 语法

    1). 条件表达式语法信息    [ 1 -eq 1 ] && echo 1        <-- 表示条件成功,执行相应操作    [ 1 -eq 1 ] || echo 1  ...

  5. 【温故知新】c#抽象类abstract与接口interface

    1.什么是抽象类 先来看MSDN对抽象类描述: 抽象类是一些留有部分或全部成员未实现的类,以便可以由派生类来提供实现. 在面向对象的编程中,抽象类用作层次结构的基类,并表示不同对象类型组的通用功能.  ...

  6. vue中遇到的坑 --- 变化检测问题(数组相关)

    最近在项目中遇到了一个问题,不知道为什么,所以最后通过动手做demo实践.查文档的方式解决了,这里做一个总结. 例1 <!DOCTYPE html> <html lang=" ...

  7. Developing crm service based on apache cxf

    1 数据库环境搭建 创建数据库boscrm 执行脚本: 脚本内容: /* Navicat MySQL Data Transfer Source Server : root Source Server ...

  8. GridView, ListView 区别

    ListView, GridView部分的类层次结构 AbsListView的xml属性 android:listSelector 当前item高亮时,显示的drawable android:draw ...

  9. nyoj 325——zb的生日——————【dp】

    zb的生日 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 今天是阴历七月初五,acm队员zb的生日.zb正在和C小加.never在武汉集训.他想给这两位兄弟买点什么 ...

  10. 分支结构if……else

    语法: if(条件) 语句或语句块1 end else begin 语句或者语句块2 end 特点: . else并不一定是必须的. . 如否条件为真,将执行语句和语句块1,条件为假时执行语句或语句块 ...