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.

题目

思路

1.  maintain stack to track all the data

2. maintain maxStack to update current max, making sure that stack.size() == maxStack.size()

3. when popMax(), use tempStack to convert data. When push data back to stack, don't forget to update maxStack at the same time.

code

 class MaxStack {
// maintain stack to track all the data
Stack <Integer> stack = new Stack<Integer>();
// maintain maxStack to update current max
Stack <Integer> maxStack = new Stack<Integer>(); public void push(int x) {
// 保证stack和maxStack的元素数量一致, 即便 x == maxStack.peek(), 也会同时push到maxStack和stack
if (maxStack.isEmpty() || x >= maxStack.peek()){
maxStack.push(x);
}
stack.push(x);
} public int pop() {
if (stack.peek().equals(maxStack.peek())){
maxStack.pop();
}
return stack.pop();
} public int top() {
return stack.peek();
} public int peekMax() {
return maxStack.peek();
} public int popMax() {
// maintain a tempStack to help convert data
Stack <Integer> tempStack = new Stack<Integer>(); int max = maxStack.peek();
// 1. push non-max item into tempStack
while (!stack.peek().equals(maxStack.peek())){
tempStack.push(stack.pop());
}
stack.pop();
maxStack.pop(); //2. directly use push() we wrote, pushing items back in both stack and tempStack
while(!tempStack.isEmpty()){
push(tempStack.pop());
}
return max;
}
}

[leetcode]716. 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 解题报告(C++)

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

  3. [LeetCode] 155. Min Stack 最小栈

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

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

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

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

  6. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

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

  7. [LeetCode] Min Stack 最小栈

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

  8. LeetCode 155 Min Stack(最小栈)

    翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检 ...

  9. LeetCode Max Stack

    原题链接在这里:https://leetcode.com/problems/max-stack/description/ 题目: Design a max stack that supports pu ...

随机推荐

  1. alert大法看执行流程(一次采坑)

    页面的dom元素加载完了,给元素一次性添加事件. 收获:事件都是一次性给添加好的,不是点击一次,................................................... ...

  2. 使用openresty && minio && thumbor 构建稳定高效的图片服务器

    备注: minio 是一个开源的s3 协议兼容的分布式存储,openresty nginx+lua 高性能可扩展的nginx 衍生版,thumbor 基于python 的图片处理服务器,支持图片的裁剪 ...

  3. Centos7.4安装配置haproxy和Keepalived补充内容

    补充比较杂 1.当master服务恢复正常之后,backup机器收到消息,然后让出vip 下面是master机器服务恢复正常后,backup机器的Keepalived日志 收到master的消息通知, ...

  4. LOJ 3059 「HNOI2019」序列——贪心与前后缀的思路+线段树上二分

    题目:https://loj.ac/problem/3059 一段 A 选一个 B 的话, B 是这段 A 的平均值.因为 \( \sum (A_i-B)^2 = \sum A_i^2 - 2*B \ ...

  5. asp.net core 基于角色的认证登陆

    一.登陆页面的Controller [Authorize(Roles = "Admin,SuperAdmin")] public class ManageController : ...

  6. Spring生态研习【三】:Spring-kafka

    1. 基本信息介绍 基于spring的kafka应用,非常简单即可搭建起来,前提是要有一个kafka的broker集群.我在之前的博文里面已经介绍并搭建了一套broker环境,参考Kafka研究[一] ...

  7. SpringBoot配置(1) 配置文件application&yml

    SpringBoot配置(1) 配置文件application&yml 一.配置文件 1.1 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application ...

  8. 廖雪峰Java6 IO编程-2input和output-6classpath资源

    1.从classpath读取文件可以避免不同环境下文件路径不一致的问题. Windows和Linux关于路径的表示不一致 Windows:C:\conf\default.properties Linu ...

  9. Scrapy学习篇(十二)之设置随机IP代理(IPProxy)

    当我们需要大量的爬取网站信息时,除了切换User-Agent之外,另外一个重要的方式就是设置IP代理,以防止我们的爬虫被拒绝,下面我们就来演示scrapy如何设置随机IPProxy. 设置随机IPPr ...

  10. [UE4]Tree View

    类似List View,但Tree View要求提供树形结构的数据.Tree View和Tile View都是继承自List View 一.创建一个名为“TreeEntry”的UserWidget,添 ...