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 ...
随机推荐
- 关于Ehcache缓存中timeToLiveSeconds和timeToIdleSeconds
[From] http://blog.csdn.net/vtopqx/article/details/8522333 闲来无事测试了下Ehcache与MemCache比较,在此发现了Ehcache中一 ...
- ssh无密码登录和scp无密码拷贝
目的:在A主机上无密码登录B主机 方法: A主机生成密钥:ssh-keygen -t rsa 将密钥复制到B主机:cat ~/.ssh/id_rsa.pub | ssh root@B 'cat > ...
- ASP.Net Core 发布ABP项目遇到的错误
1.HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 与ASP.NET时代不同,ASP.NET Core不再是由IIS工作 ...
- PIE SDK打开GDB、Dwg数据
1. 功能简介 目前不同的GIS软件平台具有自己独特支持的数据格式,如ESRI的File GeoDataBase和Personal GeoDataBase.MapInfo的mif数据.AutoCAD的 ...
- vm12下Centos6的javaweb环境搭建
配置linux的javaweb环境之前: 1.在windows安装xshell(非必需,但是推荐) 2.在linux安装Linux与windows文件传输工具RZSZ[root@192 ~]# yum ...
- 使用SVN进行源码管理
阅读目录: 1.SVN服务端配置 1.1 创建版本库 1.2 创建用户 1.3 设置用户权限 2.SVN客户端使用 2.1 向SVN服务器中导入源码 2.1.1 直接通过TortoiseSVN向SVN ...
- Python 中的反射和自省
本文主要介绍Python中的反射和自省,以及该机制的简单应用 熟悉Java的程序员,一定经常和Class.forName打交道.即使不是经常亲自调用这个方法,但是在很多框架中(spring,eclip ...
- TOJ 1717 WOJ
描述 Alex likes solving problems on WOJ (http://acm.whu.edu.cn/oak). As we all know, a beautiful ballo ...
- React.js 小书 Lesson9 - 事件监听
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson9 转载请注明出处,保留原文链接和作者信息. 在 React.js 里面监听事件是很容易的事情 ...
- HTML的注释方式对JSP的JSTL不管用
<fmt:parseNumber var="y" integerOnly="true" type="number" value=&qu ...