LeetCode Max Stack
原题链接在这里:https://leetcode.com/problems/max-stack/description/
题目:
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.
题解:
两个stack来解决. 不同的是popMax, 用temp stack 来保存找到最大值之前的, pop出max后再加回去, 同时更新maxStk.
Note: when popMax, get temp back, it needs to update maxStk as well.
Time Complexity: push, O(1). pop, O(1). top, O(1). peekMax, O(1). popMax, O(n). n是stack中数字的个数.
Space: O(n).
AC Java:
class MaxStack {
Stack<Integer> stk;
Stack<Integer> maxStk;
/** initialize your data structure here. */
public MaxStack() {
stk = new Stack<Integer>();
maxStk = new Stack<Integer>();
}
public void push(int x) {
stk.push(x);
if(maxStk.isEmpty() || maxStk.peek()<=x){
maxStk.push(x);
}
}
public int pop() {
int x = stk.pop();
if(!maxStk.isEmpty() && x==maxStk.peek()){
maxStk.pop();
}
return x;
}
public int top() {
return stk.peek();
}
public int peekMax() {
return maxStk.peek();
}
public int popMax() {
Stack<Integer> tempStk = new Stack<Integer>();
int x = maxStk.pop();
while(!stk.isEmpty() && stk.peek()<x){
tempStk.push(stk.pop());
}
stk.pop();
while(!tempStk.isEmpty()){
int top = tempStk.pop();
push(top);
}
return x;
}
}
/**
* 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();
*/
类似Min Stack.
LeetCode 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 最大栈
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] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode——Max Consecutive Ones
LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...
- 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] Max Chunks To Make Sorted II 可排序的最大块数之二
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
- LeetCode Monotone Stack Summary 单调栈小结
话说博主在写Max Chunks To Make Sorted II这篇帖子的解法四时,写到使用单调栈Monotone Stack的解法时,突然脑中触电一般,想起了之前曾经在此贴LeetCode Al ...
随机推荐
- [CTSC2011]幸福路径
题目描述 有向图 G有n个顶点 1, 2, …, n,点i 的权值为 w(i).现在有一只蚂蚁,从 给定的起点 v0出发,沿着图 G 的边爬行.开始时,它的体力为 1.每爬过一条 边,它的体力都会下降 ...
- Python3:Django连接Mysql数据库时出错,'Did you install mysqlclient or MySQL-python?'
Python3:Django连接Mysql数据库时出错,'Did you install mysqlclient or MySQL-python?' 一.原因 因为Python版本问题,MySQLdb ...
- Python 中lambda 简单介绍
转自:https://www.cnblogs.com/AlwaysWIN/p/6202320.html 在学习python的过程中,lambda的语法经常出现,现在将它整理一下,以备日后查看. 1.l ...
- 【React Native开发】React Native进行签名打包成Apk
转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/50525976 本文出自:[江清清的博客] (一)前言 [好消息]个人 ...
- Linux常用监控命令
A goal is a dream with a deadline. Much effort, much prosperity. 1.IOSTAT命令 此命令安装包为sysstat 可用yu ...
- COS-6主存管理
操作系统是用户和计算机的接口,同时也是计算机硬件和其他软件的接口.操作系统的功能包括管理计算机系统的硬件.软件及数据资源,控制程序运行,改善人机界面,为其它应用软件提供支持,让计算机系统所有资源最大限 ...
- Linux系统运行级别配置
Linux的运行级别 Linux的运行级别有七种,可以通过查看/etc/inittab文件进行了解: Level0:系统停机状态,默认系统运行级别不能设置为0,否则系统不能正常启动: Level1:单 ...
- Spring_配置 Bean(1)
- Spring 模块
- iOS开发进阶 - 使用Carthage管理iOS第三方库
移动端访问不佳,请访问我的个人博客 最近在研究Swift,一不小心发现一个好的的管理iOS第三方库Carthage,就跟第一次使用CocoaPods时一样兴奋不已,在研究了大半天后终于能用了,使用起来 ...