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 ...
随机推荐
- python的正则表达式捕获组命名问题
- 自定义QSS
/*QComboBox*/ QComboBox {border: 1px solid rgb(111, 156, 207);border-radius: 3px;padding: 1px 18px 1 ...
- ubuntu 14.04 建立wifi热点
昨天突然想起来我可以用笔记本搞一个热点这样我的手机就不用上流量了,但是手机死活搜不到建好的信号,目前的解决方案如下: 直接用ubuntu自带的创建wifi网络功能是不好使的,因为android系统不支 ...
- java--何时处理Exception(哪一个层级),包装的基础类处理任务尽可能简洁,写入日志,检查null等运行时异常
1. 运行时异常和受检异常 2. 提前预防运行时异常.最常发生的是NPE,而检查NPE是程序员的基本职责.其他的,如除0等运行时异常的检查,需要程序员仔细检查,每个函数都得检查(除非可以确定不会有空指 ...
- 运行【guns】spring boot 的四种方式
IDE 运行 运行带有main方法类 用mvn运行Spring-boot项目 在父项目中运行 mvn clean mvn install 在主项目中运行 mvn spring-boot:run 用JA ...
- NumPy来自数值范围的数组
NumPy - 来自数值范围的数组 这一章中,我们会学到如何从数值范围创建数组. numpy.arange 这个函数返回ndarray对象,包含给定范围内的等间隔值. numpy.arange(sta ...
- JNIjw03
1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw03.h" JNIEXPORT void JNICALL ...
- 谈谈你对Glide和Picasso他们的对比的优缺点
1.Picasso和Glide的withi后面的参数不同 Picasso.with(这里只能传入上下文) . Glide.with,后面可以传入上下文,activity实例,FragmentA ...
- spring mvc:文本框
采用:<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> ...
- 为啥YII2 会出现 mcrypt_generic_init(): Key size is 0
解决方案如下: (关键) 示例代码中,加密解密类的实例创见通过 Class 同名方法 的方式创建. public function WXBizMsgCrypt($token, $encodingAes ...