[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 ...
随机推荐
- PythonStudy——流程控制 Process control
1. 分支结构 -- if -- if...else...-- if...elif...else...-- if嵌套 ''' if 条件: 同缩进的代码块 ''' if age > 18: pr ...
- day04-Python的流程控制
- Docker之数据卷Volume(七)
一.简介 Docker数据卷(volume)机制.volume是存在于一个或多个容器中的特定文件或文件夹,这个目录以独立于联合文件系统的形式在宿主机中存在,并为数据的共享与持久化提供便利. 1)v ...
- Padavan老毛子固件:17CE插件集成
Padavan老毛子固件:17CE插件集成 1.老毛子路由设置:系统管理-服务-启动SSH服务器 以下链接下载 "winscp" http://down.orsoon.co ...
- Linux中加速访问github
1. 登录http://tool.chinaz.com/dns/ 2. 查询以下域名映射,并分别取访问速度较快的一个ip github.global.ssl.fastly.net -> ...
- cmd下PUSHD和POPD命令使用说明
PUSHD命令保存当前目录以供 POPD 命令使用,然后改到指定的目录. PUSHD [path | ..] path 指定要成为当前目录的目录. 如果命令扩展被启用,除了一般驱动器号和路径,PUSH ...
- Oracle 关于concat与双竖线用法的补充
--只能连接2个字符串select concat('nod',' chen is ') from dual; --连接2个列名select concat(name,ip2) from vm_info; ...
- linux6下源码安装mysql5.6
概述:CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14.正文:一:卸载旧版本使用下面的命令检查是否安装有MySQL Serverrpm ...
- Struts2 中常用的代码
BaseAction public class BaseAction extends ActionSupport { protected String target; public Map getRe ...
- Redis详解入门篇
Redis详解入门篇 [本教程目录] 1.redis是什么2.redis的作者3.谁在使用redis4.学会安装redis5.学会启动redis6.使用redis客户端7.redis数据结构 – 简介 ...