[leetcode]716. Max 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. maintain stack to track all the data
2. maintain maxStack to update current max, making sure that stack.size() == maxStack.size()
3. when popMax(), use tempStack to convert data. When push data back to stack, don't forget to update maxStack at the same time.






code
class MaxStack {
// maintain stack to track all the data
Stack <Integer> stack = new Stack<Integer>();
// maintain maxStack to update current max
Stack <Integer> maxStack = new Stack<Integer>();
public void push(int x) {
// 保证stack和maxStack的元素数量一致, 即便 x == maxStack.peek(), 也会同时push到maxStack和stack
if (maxStack.isEmpty() || x >= maxStack.peek()){
maxStack.push(x);
}
stack.push(x);
}
public int pop() {
if (stack.peek().equals(maxStack.peek())){
maxStack.pop();
}
return stack.pop();
}
public int top() {
return stack.peek();
}
public int peekMax() {
return maxStack.peek();
}
public int popMax() {
// maintain a tempStack to help convert data
Stack <Integer> tempStack = new Stack<Integer>();
int max = maxStack.peek();
// 1. push non-max item into tempStack
while (!stack.peek().equals(maxStack.peek())){
tempStack.push(stack.pop());
}
stack.pop();
maxStack.pop();
//2. directly use push() we wrote, pushing items back in both stack and tempStack
while(!tempStack.isEmpty()){
push(tempStack.pop());
}
return max;
}
}
[leetcode]716. Max Stack 最大栈的更多相关文章
- [LeetCode] Max Stack 最大栈
Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...
- 【LeetCode】716. Max Stack 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双栈 日期 题目地址:https://leetcode ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 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 ...
- 716. Max Stack实现一个最大stack
[抄题]: Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode 155 Min Stack(最小栈)
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...
- LeetCode Max Stack
原题链接在这里:https://leetcode.com/problems/max-stack/description/ 题目: Design a max stack that supports pu ...
随机推荐
- MySQL Error--打开过多文件导致数据库无法连接
[此文抄自同事的邮件,当作笔记学习] 环境描述Mysql 5.5.21OS centos 5.8zabbix agent 2.4.3 情况描述现象数据库处于运行状态,但是无法创建新的连接,监控报警数据 ...
- oracle-企业信息化
http://www.itpub.net/thread-1873735-1-1.html OCP考试心得 http://blog.csdn.net/rlhua/article/detai ...
- bzoj 1814 Ural 1519 Formula 1 ——插头DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...
- 第一章 C#入门 (Windows窗体应用程序)(一)
我的第一个窗体应用程序(一) [案例说明] 在文本框中显示一行文字“Hello C#!”,单击[显示]按钮后在文本框中显示文字:单击[清除]按钮后清除文本框中的内容. [案例实现步骤] 1.新建项目 ...
- Java_集合面试题
Java_集合面试题 0.链表,队列和栈的区别? 链表是一种存储结构,指得是存储时候除了要存储数据元素之外,还要用数据元素一起的另外空间存储数据元素的关系. 队列和栈都是线性表,属于逻辑结构范畴,都是 ...
- sql server 2016 附加 其它目录的数据库
如果数据库不在默认目录,那么需要将 mdf所在目录或者 mdf文件 添加 用户 [NT SERVICE\MSSQLSERVER]的创建权限,否则会提示没有权限, 具体详见: https://docs. ...
- 理解OpenShift(1):网络之 Router 和 Route
理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...
- Django中media的配置
Django中media的配置 Django中media文件夹是我们文件(比如头像.文件.视频等)数据十分重要的存放处,这里以用户头像的上传以及media文件的访问为例为大家详细讲解下media的相关 ...
- studio--常见设置
13.Butterknife插件:zelezny 12.android studio怎么设置打开项目时打开项目列表? 11.stuido 代码背景颜色设置为护眼模式 ======== 13.But ...
- 爬虫:输入网页之后爬取当前页面的图片和背景图片,最后打包成exe
环境:py3.6 核心库:selenium(考虑到通用性,js加载的网页).pyinstaller 颜色显示:colors.py colors.py 用于在命令行输出文字时,带有颜色,可有可无. # ...