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 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.
1. Solution: add popmax this func, you can pop out max at any time , so max stack to track the max elements does not work as minstack(leetcode 155) did
add one more stack for helping
1. pop out the ele in the origin stack until find the max
2. push all into temp stack
3. use push(self built func) to push all the ele from temp stack into original stack
class MaxStack {
//becasuse of popMAx, two stacks is not enough
List<Integer> s1 ;//store ele
List<Integer> s2 ;//store min ele
/** initialize your data structure here. */
public MaxStack() {
s1 = new ArrayList<Integer>();
s2 = new ArrayList<Integer>();
}
public void push(int x) {
s1.add(x);
if(s2.isEmpty() || s2.get(s2.size()-1) <= x) s2.add(x);
}
public int pop() {
if(s1.isEmpty()) return 0;
int ele = s1.remove(s1.size()-1);
if(!s2.isEmpty() && ele == s2.get(s2.size()-1)){
s2.remove(s2.size()-1);
}
return ele;
}
public int top() {
if(!s1.isEmpty())
return s1.get(s1.size()-1);
return 0;
}
public int peekMax() {
if(!s2.isEmpty())
return s2.get(s2.size()-1);
return 0;
}
//handful problem
public int popMax() {
//pop max
if(!s1.isEmpty() && !s2.isEmpty()){
int ele = s2.remove(s2.size() - 1);
List<Integer> s3 = new ArrayList<>();
while(ele != s1.get(s1.size() - 1)){
int temp = s1.remove(s1.size() - 1);
s3.add(temp);
}
s1.remove(s1.size() - 1);
while(!s3.isEmpty()){
push(s3.remove(s3.size()-1));
}
return ele;
}
return 0;
}
}
/**
* 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();
*/
2. solution 2: https://leetcode.com/problems/max-stack/discuss/153748/Java-LinkedList-and-PriorityQueue (using linkedlist and priorityQueue)
Leave a comment u have a question!
716. Max Stack (follow up questions for min stack)的更多相关文章
- 带最小值操作的栈 · Min Stack
[抄题]: 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. [思维问题]: [一句话思 ...
- [leetcode]716. Max Stack 最大栈
Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
随机推荐
- [转] Java运行时动态生成class的方法
[From] http://www.liaoxuefeng.com/article/0014617596492474eea2227bf04477e83e6d094683e0536000 廖雪峰 / 编 ...
- 0前端 框架 库_千万别去碰js呀 混合APP_webAPP_美工 选有类型的语言,比如TypeScript
常用知识点,技巧 添加库到本地: (举例 element-ui) 用npm命令行把包下载到本地 在电脑里找到资源文件,比如 C:\Users\XiaoCong\AppData\Roaming\npm\ ...
- SQL手工注入学习 一
sql注入: (基于DVWA环境的sql注入) 流程: 1.判断是否有SQL注入漏洞 2.判断操作系统.数据库和web应用的类型 3.获取数据库信息看,包括管理员信息(拖库) ...
- [转]how to use both JDK 7 and JDK 8 in one build
Note: This article is original from https://gist.github.com/aslakknutsen/9648594 JDK 8 Released Most ...
- java多线程之线程组与线程池
看这篇文章:http://blog.csdn.net/zen99t/article/details/50909099
- (一)环境安装之Java
一.安装JDK 点击 JDK8下载,根据自己的平台,选择相应的版本进行下载. Java环境分JDK和JRE ,JDK就是Java Development Kit. 简单的说JDK是面向开发人员使用的S ...
- JS常用的设计模式(4)——适配器模式
去年年前当时正在开发dev.qplus.com, 有个存储应用分类id的js文件, 分类id的结构最开始设计的比较笨重. 于是我决定重构它. 我把它定义成一个json树的形式, 大概是这样: var ...
- s中的闭包
今天看了关于js闭包方面的文章,还是有些云里雾里,对于一个菜鸟来说,学习闭包确实有一定的难度,不说别的,能够在网上找到一篇优秀的是那样的不易. 当然之所以闭包难理解,个人觉得是基础知识掌握的不牢,因为 ...
- [转]浅谈微信小程序
本文转自:http://www.cnblogs.com/liziyou/p/6340159.html 微信小程序 1.什么是小程序 小程序是指微信公众号平台小程序,小程序可以在微信内被便捷的获取和转播 ...
- Java集合一
java的集合类主要由两个接口派生而出:Collection && Map 这两个接口是集合框架的根接口 Collection----直接派生:Set(无序集合,元素不可重复) Lis ...