原题链接在这里:https://leetcode.com/problems/max-stack/description/

题目:

Design a max stack that supports push, pop, top, peekMax and popMax.

  1. push(x) -- Push element x onto stack.
  2. pop() -- Remove the element on top of the stack and return it.
  3. top() -- Get the element on the top.
  4. peekMax() -- Retrieve the maximum element in the stack.
  5. 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:

  1. -1e7 <= x <= 1e7
  2. Number of operations won't exceed 10000.
  3. 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的更多相关文章

  1. [LeetCode] Max Stack 最大栈

    Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...

  2. [leetcode]716. Max Stack 最大栈

    Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...

  3. 【LeetCode】716. Max Stack 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双栈 日期 题目地址:https://leetcode ...

  4. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

  6. 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 ...

  7. 716. Max Stack实现一个最大stack

    [抄题]: Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x ...

  8. [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二

    This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...

  9. LeetCode Monotone Stack Summary 单调栈小结

    话说博主在写Max Chunks To Make Sorted II这篇帖子的解法四时,写到使用单调栈Monotone Stack的解法时,突然脑中触电一般,想起了之前曾经在此贴LeetCode Al ...

随机推荐

  1. Java:执行jar文件命令

    Java:执行jar文件命令 执行jar文件命令: java -jar test.jar win7系统切换目录命令: cd /d d:/test

  2. winform webbrowser禁用网页target=blank

    /// <summary> /// 屏蔽target=_blank 的弹出窗口 /// </summary> /// <param name="sender&q ...

  3. invalid derived query的解决办法

    标签: eclipse / invalid / derived / 解决办法 / 校验功能 479 在Eclipse的运行过程中,突然有一个接口跳出如下错误: invalid derived quer ...

  4. C3p0的参数

    C3p0的参数设置:ComboPooledDataSource和BasicDataSource一样提供了一个用于关闭数据源的close()方法,这样我们就可以保证Spring容器关闭时数据源能够成功释 ...

  5. IDEA 编译时 未结束的字符串文字

    这个问题就是编码的问题,修改文件的编码可以解决 1. IDEA中   file-->Settings 找到File Encodings,将IDE Encoding.Project Encodin ...

  6. idea技巧

    写在前面 以前一直用的elipce,如今入坑IntelliJ IDEA,没想到啊.深深的爱上了它,强大到无所不能: "工欲善其事必先利其器",IntelliJ IDEA作为一个非常 ...

  7. yii2:多条件多where条件下碰到between时,between语句如何处理呢?

    yii2:多条件多where条件下碰到between时,between语句如何处理呢? 我有一张表:id,name,telphone,ticket_no,status,create_time等字段, ...

  8. python Tkinter图形用户编程简单学习(一)

    Events(事件) Events are given as strings, using a special event syntax:事件以字符串的方式给出,使用特殊的事件语法:<modif ...

  9. Oracle归档模式与非归档模式设置

    (转自:http://www.cnblogs.com/spatial/archive/2009/08/01/1536429.html) Oracle的日志归档模式可以有效的防止instance和dis ...

  10. python基础之协程,列表生成式,生成器表达式

    三元表达式 #三元表达式 name = 'alex' name = 'egon' res='SB' if name == 'alex'else 'shuai' print(res)#输出:shuai ...