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 ...
随机推荐
- h5新特性--- 多媒体元素
在H5中只有一行代码即可实现在页面中插入视频 <video src="插入的视频的名字" controls></video> 可以指明视频的宽度和高度 &l ...
- qt项目: error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1900”不匹配值“1800”
error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1900”不匹配值“1800” 该错误 网上通常的解释是: 原因:由于你使用了vs2012,相比较vs2010以及之前的vs ...
- 20145235李涛《网络对抗》Exp5 MSF基础应用
基础问答 用自己的话解释什么是exploit,payload,encode? exploit:相当于搬运工,把攻击代码传送到靶机中. payload:相当于shellcode. encode:相当于包 ...
- HTTP响应代码集合
用于表示临时响应并需要请求者执行操作才能继续的状态代码.代码说明100(继续)请求者应当继续提出请求.服务器返回此代码则意味着,服务器已收到了请求的第一部分,现正在等待接收其余部分. 101(切换协议 ...
- 【bzoj1115】[POI2009]石子游戏Kam(博弈论)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1115 观察问题,我们能发现前后相邻两堆石子的数量差一定非负,而我们在第i堆石子中移走k ...
- bzoj 3884 上帝与集合的正确用法 指数循环节
3884: 上帝与集合的正确用法 Time Limit: 5 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 根据一些 ...
- Webstorm常用快捷键备忘(Webstorm入门指南)
WebStorm 是jetbrains公司旗下一款JavaScript 开发工具.被广大中国JS开发者誉为“Web前端开发神器”.“最强大的HTML5编辑器”.“最智能的JavaSscript IDE ...
- 第二章 Burp Suite代理和浏览器设置
Burp Suite代理工具是以拦截代理的方式,拦截所有通过代理的网络流量,如客户端的请求数据.服务器端的返回信息等.Burp Suite主要拦截http和https协议的流量,通过拦截,Burp S ...
- Go-gin CORS 跨域中间件
原文:https://stackoverflow.com/questions/29418478/go-gin-framework-cors func CORSMiddleware() gin.Hand ...
- InnoDB存储引擎的B+树索引算法
关于B+树数据结构 ①InnoDB存储引擎支持两种常见的索引. 一种是B+树,一种是哈希. B+树中的B代表的意思不是二叉(binary),而是平衡(balance),因为B+树最早是从平衡二叉树演化 ...