[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 ...
随机推荐
- dubbo 基础入门
一.什么是dubbo? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,说白了就是个远程服务调用的分布式框架. dubbo产生的背景 ① 单一 ...
- PS 给照片换背景
1. 打开一张照片,导入证件照 2. 点击选择 => 选择并遮住 (快捷键 command + option + r) 3. 点击快速选择工具,将属性设置里面的视图模式选择为洋葱皮,鼠标点击需要 ...
- VS2010+WPF+LINQ for MySQL
学习wpf,连接数据库和linq for mysql 1.参考以前博文,恢复在 Vs2010+linQ for Mysql的环境. 2.建立 wpf工程,参照1,生成 datacontext.cs , ...
- Java学习路线思维导图
- QQ群成员发言次数统计(正则表达式版)
1.先将QQ群的消息记录以.txt文件格式导出来,保存路径及名称自己定义(在本文我导出到Y盘,命名为test.txt) 2.程序如下: data statistics1; if _n_=1 then ...
- MPP数据库
MPP数据库 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/lyc417356935/article/details/45033069 MPP数据库定 ...
- C# 操作符 << 与 >>
1.<< 左移操作符: 左移操作符,将第一个操作数向左移动第二个操作数指定的位数,空出的位置补0.左移相当于乘. 左移一位相当于乘2;左移两位相当于乘4;左移三位相当于乘8. 如:x< ...
- 几种将上一个请求的cookies带入下一个请求中的方法
*** 此次练习不包含使用requests.session()方法实现: 练习环境:本地安装禅道 格式: 1.头部传Cookie:xxx2.加参数cookies=字典格式3.直接传RequestsCo ...
- qt4 看不到qstring内容
qt4: https://gist.github.com/gregseth/9bcd0112f8492fa7bfe7
- 深入学习 Java 序列化
前言 对于Java的序列化,一直只知道只需要实现Serializbale这个接口就可以了,具体内部实现一直不是很了解,正好这次在重复造RPC的轮子的时候涉及到序列化问题,就抽时间看了下 Java序列化 ...