716. Max Stack (follow up questions for min stack)
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)的更多相关文章
- 带最小值操作的栈 · Min Stack
[抄题]: 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. [思维问题]: [一句话思 ...
- [leetcode]716. Max Stack 最大栈
Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [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 ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
随机推荐
- 01背包----简单DP
描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励的时刻了! 小Ho现在手上有M张奖券,而奖品区有N件奖品,分别标号为1到N,其中第i件奖品需要ne ...
- Mac上Node环境配置
公司配备Mac笔记本,以前没用过mac开发项目,一开始依然是从node官网下载安装包,后来领导说最好是用brew安装软件,这样比较方便,安装和卸载,只要在命令行输入相应的 install 和 unin ...
- h5 的video视频控件
h5 的video视频控件 由于html5的流行,其中的video视频的使用很流行,使得可恨的IE9也能与时俱进了. video所支持的格式有mp4.ogg和wav三种. 例: HTML5 Video ...
- mysql explain工具使用
explain工具可以确认执行计划是否良好,查询是否走了合理的索引.查询的执行计划,随着数据的变化也可能会有变化.调用方式:explain + [sql语句]. 另外,explain是有局限性的:1. ...
- Gradle发布项目到 maven 之novoda/bintray-release(3)
novoda/bintray-release 使用这个插件上传比较简单,只需要两步就可以 1.在项目根目录下的 build.gradle 添加插件依赖 // Top-level build file ...
- (转)淘宝系统信息采集和监控工具tsar
淘宝系统信息采集和监控工具tsar 项目地址:https://github.com/alibaba/tsar 一.介绍 Tsar是淘宝的系统信息采集和监测工具,主要用来收集服务器的系统信息(如cpu, ...
- Tomcat服务器安装
Tomcat服务器类似于XAMPP,主要安装步骤如下. 第一步: 安装JDK. 第二步: 安装tomcat. 第三步: 启动tomcat下bin下的startup.bat即可启动tomcat. 可能出 ...
- Scala安装及开发环境搭建
最近想学习下scala,为后面转大数据做一些沉淀. 1. 首先保证jdk已经成功安装 2. 去官网下载scala安装程序 http://www.scala-lang.org/download/all. ...
- keepalive学习之软件设计
软件架构如下图所示: Keepalived 完全使用标准的ANSI/ISO C写出. 该软件主要围绕一个中央I/O复用分发器而设计,这个I/O复用分发器提供网络实时功能. 主要设计目标着重于从所有的模 ...
- Jquery实现自动生成二级目录
在博客园开通博客以后,就看到某位博友写的js自动生成目录的文章,当时觉得生成目录能给阅读带来方便,所以就直接拿来使用了.用了一段时间以后,发现只能生成一级目录,不能生成多级目录,有点美中不足.所以想着 ...