原题链接在这里: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. Apache 错误整理

    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localho ...

  2. Windows 修改个性化时间显示

    A goal is a dream with a deadline. Much effort, much prosperity. 我感觉我的时间显示不够人性化.不够个性化 修改注册表 我的系统为Win ...

  3. bzoj 2748: [HAOI2012]音量调节

    2748: [HAOI2012]音量调节 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 872  Solved: 577[Submit][Status] ...

  4. springboot创建多环境profile打包

    springboot开发打包时,一般会有多个环境,dev,qa,prod等,配置文件大多雷同,只是方便开发切换,但是生成部署时产生的war包就无需这么多重复配置了,这时这些dev,qa的配置就不应该打 ...

  5. Docker高级使用

    Docker卸载应用程序 先删除容器,在删除镜像 查询容器 docker ps –a 使用容器id删除容器 docker rm 18e672ecd8ed 查询镜像 docker images 使用镜像 ...

  6. “玲珑杯”ACM比赛 Round #13 B -- 我也不是B(二分排序)

    题意:开始有一个空序列s,一个变量c=0,接着从左往右依次将数组a中的数字放入s的尾部,每放一个数字就检测一次混乱度K,当混乱度k大于M时就清空序列并让c=c+1 K = Bi * Vi(1<= ...

  7. NumPy线性代数

    NumPy - 线性代数 NumPy 包包含numpy.linalg模块,提供线性代数所需的所有功能. 此模块中的一些重要功能如下表所述. 序号 函数及描述 1. dot 两个数组的点积 2. vdo ...

  8. iscroll.js的简单使用方法(总结)

    iscroll.js的简单使用方法(总结) 一.总结 一句话总结:Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化. 最佳的HTML结构如下: <div id="wrap ...

  9. Linux文件夹权限详解

    - 第一个字符代表文件(-).目录(d),链接(l) - 其余字符每3个一组(rwx),读(r).写(w).执行(x) - 第一组rwx:文件所有者的权限是读.写和执行 - 第二组rw-:与文件所有者 ...

  10. hdu 5979 Convex(水,求面积)

    Convex Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...