155. Min Stack
题目:
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
链接: http://leetcode.com/problems/min-stack/
题解:
最小栈。这道题在电面Bloomberg的supply chain组的Senior SDE时遇到了原题,秒杀掉了。然后面试的老印很坏,follow up说你这个不是stack, 说想要new stack<>()的时候自带min功能,跟我绕了半天,故意误导, 好恶心...最后果然电面没过-_____-!! 后来看了CC150, 才知道这老印也许想要的是CC150的写法 - MinStack extends Stack<Integer>, 然后用minStack和super进行互动,也是两个栈。
又聊远了,这道题目,保持stack的功能同时还要有getMin()。一般的方法是维护两个stack。 也可以维护一个stack,但要创建一个Node class,空间复杂度并不能被节省。 还有的做法是用一个stack,stack里存min到当前值x的距离,然后有些计算,比较巧妙。
维护两个stack, Time Complexity (pop,push,getMin,top) - O(1) , Space Complexity - O(n)。
class MinStack {
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> minStack = new Stack<>(); public void push(int x) {
if(minStack.isEmpty() || x <= minStack.peek())
minStack.push(x);
stack.push(x);
} public void pop() {
if(stack.peek().equals(minStack.peek()))
minStack.pop();
stack.pop();
} public int top() {
return stack.peek();
} public int getMin() {
return minStack.peek();
}
}
维护一个栈, Time Complexity (pop,push,getMin,top) - O(1) , Space Complexity - O(n)。
class MinStack {
private Stack<Node> stack; public MinStack() {
this.stack = new Stack<>();
} public void push(int x) {
int min = Math.min(x, getMin());
this.stack.push(new Node(x, min));
} public void pop() {
this.stack.pop();
} public int top() {
return this.stack.peek().val;
} public int getMin() {
if(this.stack.isEmpty())
return Integer.MAX_VALUE;
return this.stack.peek().min;
} private class Node {
public int min;
public int val; public Node(int val, int min) {
this.val = val;
this.min = min;
}
}
}
二刷:
除了维护两个栈的方法以外,还有两种方法, 一种是建立一个Node同时保存当前值和最小值。
另外一种是reeclapple的写法。
- 设定一个min,push的时候,除了第一个数外,只把每个数x和min的差值(x - min)存入栈。当 x< min的时候更新min = x
- pop的时候pop出这个差值,假如这个值小于0,我们更新min = min - pop,否则min不变
- 计算peek的时候,先peek出栈顶元素, 假如这个元素top大于0,那么返回 top + min, 否则返回min。这里理解比较tricky.
- 计算getMin的时候直接返回min
Java:
最简单的维护两个stack的。
class MinStack {
private Stack<Integer> minStack = new Stack<>();
private Stack<Integer> stack = new Stack<>(); public void push(int x) {
stack.push(x);
if (minStack.isEmpty() || x <= minStack.peek()) {
minStack.push(x);
}
} public void pop() {
int x = stack.pop();
if (x == minStack.peek()) {
minStack.pop();
}
} public int top() {
return stack.peek();
} public int getMin() {
return minStack.peek();
}
}
三刷:
要注意假如遇到的话,还要加上各种边界条件判断以及throw EmptyStackException
Java:
class MinStack {
private Stack<Integer> stack = new Stack<>();
private Stack<Integer> minStack = new Stack<>(); public void push(int x) {
stack.push(x);
if (minStack.isEmpty() || x <= minStack.peek()) minStack.push(x);
} public void pop() {
int x = stack.pop();
if (x == minStack.peek()) minStack.pop();
} public int top() {
return stack.peek();
} public int getMin() {
return minStack.peek();
}
}
使用一个Stack:
每次min要被更新的时候,先存入min,再存入新元素。相当于保持一个递减的min序列。 假如elements递减,则栈内元素类似 {min 1, elem1, min2, elem2, min3, elem3}等等,
- 这里在insert的时候,假如x <= min,则我们先把之前被更新过的min放进stack里, 把min设为当前的x, 之后把x放入栈
- 在pop的时候, 我们pop出一个元素,然后跟min比较,假如等于min,那说明下一个元素是旧的min,我们更新min为 stack.pop(), 也把这个旧的min pop出来
- top 直接返回 stack.peek();
- getMin直接返回min
class MinStack {
private Stack<Integer> stack = new Stack<>();
int min = Integer.MAX_VALUE; public void push(int x) {
if (x <= min) {
stack.push(min);
min = x;
}
stack.push(x);
} public void pop() {
int peek = stack.pop();
if (peek == min) min = stack.pop();
} public int top() {
return stack.peek();
} public int getMin() {
return min;
}
}
Update:
许多题目现在必须追求最优解了 -____-!!
public class MinStack {
private Stack<Integer> stack = new Stack<>();
private int min = Integer.MAX_VALUE /** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
} public void push(int x) {
if (x <= min) {
stack.push(min);
min = x;
}
stack.push(x);
} public void pop() {
int top = stack.pop();
if (top == min) min = stack.pop();
} public int top() {
return stack.peek();
} public int getMin() {
return min;
}
} /**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
Reference:
http://www.geeksforgeeks.org/design-and-implement-special-stack-data-structure/
https://leetcode.com/discuss/21071/java-accepted-solution-using-one-stack
https://leetcode.com/discuss/15679/share-my-java-solution-with-only-one-stack
https://leetcode.com/discuss/19389/java-solution-accepted
https://leetcode.com/discuss/23927/smart-accepted-java-solution-linked-list
https://leetcode.com/discuss/16029/shortest-and-fastest-1-stack-and-2-stack-solutions
155. Min Stack的更多相关文章
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- Java for LeetCode 155 Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【leetcode】155 - Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LeetCode题解 #155 Min Stack
写一个栈,支持push pop top getMin 难就难在在要在常量时间内返回最小的元素. 一开始乱想了很多东西,想到了HashMap,treeMap,堆什么的,都被自己一一否决了. 后来想到其实 ...
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- LC 155 Min Stack
问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
随机推荐
- SQL Server调优系列进阶篇 - 如何索引调优
前言 上一篇我们分析了数据库中的统计信息的作用,我们已经了解了数据库如何通过统计信息来掌控数据库中各个表的内容分布.不清楚的童鞋可以点击参考. 作为调优系列的文章,数据库的索引肯定是不能少的了,所以本 ...
- ###《Effective STL》--Chapter6
点击查看Evernote原文. #@author: gr #@date: 2014-09-27 #@email: forgerui@gmail.com Chapter6 函数子.函数子类.函数及其他 ...
- C++输入结束
通过判断输入是否等于EOF,可以结束输入. EOF 是个宏,其意思是:End Of File,文件尾标志. 从数值上来看,就是整数-1. 在C语言的头文件中对其进行了宏定义: libio.h: ...
- HDOJ 1024 Max Sum Plus Plus -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Problem Description Now I think you have got an ...
- windows server 2008 集成raid卡驱动
给服务器安装2008系统,一般都需要通过引导盘和操作系统盘来进行安装,安装过程比较繁琐时间也比较长,于是就想做一个集成了服务器驱动的2008系统盘,这样就可以直接用光盘安装,简单方便,第一步需要解决的 ...
- jquery ajax php+mysql 无刷新分页 详细实例
最近在接触jquery和ajax,当前项目也会用到分页,为了用户体验更好一些,就准备用无刷新分页,这个demo很适合新手学习查看,写的比较清晰,话不多说,直接上代码吧. 首先是html页面,index ...
- Java知识总结--CoreJava
在网上看到的关于Java的知识总结,觉得很受用,分享给大家..... 如果有什么错误,也欢迎指正批评. 1 简述下java基本数据类型及所占位数,java基本数据类型:4类8种 整数类型:byte(1 ...
- Spring.Net AOP实例
Spring.Net和Log4net.NUnit.NHibernate一样,也是先从Java中流行开来,然后移植到了.NET当中,形成了.NET版的Spring框架.其官方网站为:http://www ...
- Sublime Text 中使用Git插件连接GitHub
sublime Text的另一个强大之处在于它提供了非常丰富的插件,可以帮助程序员来适合大多数语言的开发.这些插件通过它自己的Package Controll(包管理)组件来安装,非常方便.一般常用的 ...
- Yii通过控制台命令创建定时任务
假设Yii项目路径为 /home/apps/ 1. 创建文件 /home/apps/protected/commands/crons.php <?php $yii = '/home/apps/f ...