Min Stack 解答
Question
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.
Solution
Original thinking is to use two stacks, one to store input elements, and the other is to store current min value.
However, there is an improvement that only use one stack.
This stack is to store diff between input value and current min value.
if current value < min value, we set min as current imput value.
Therefore, when we pop or peek each element in stack, we know:
If it's greater than 0, then it must be greater than current min value.
If it's smaller than 0, then it must equal to current min value.
class MinStack {
Stack<Long> diff;
private long min;
public MinStack() {
min = Integer.MAX_VALUE;
diff = new Stack<Long>();
}
public void push(int x) {
diff.push((long)x - min);
min = x < min? x : min;
}
public void pop() {
if (diff.size() < 1)
return;
long tmp = diff.pop();
if (tmp < 0)
min -= tmp;
}
public int top() {
long tmp = diff.peek();
if (tmp < 0)
tmp = min;
else
tmp += min;
return (int)tmp;
}
public int getMin() {
return (int)min;
}
}
注意这里stack和min的类型都应该是long,否则会有越界问题!
Min Stack 解答的更多相关文章
- [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 ...
- 155. Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
随机推荐
- HDu 5433 Xiao Ming climbing (BFS)
题意:小明因为受到大魔王的诅咒,被困到了一座荒无人烟的山上并无法脱离.这座山很奇怪: 这座山的底面是矩形的,而且矩形的每一小块都有一个特定的坐标(x,y)和一个高度H. 为了逃离这座山,小明必须找到大 ...
- OS error set
Failed to resolve/decode supposed IPv4 source addres Failed to resolve/decode supposed IPv4 source a ...
- Spring MVC基础
1.Web MVC基础 MVC的本质是表现层模式,我们以视图模型为中心,将视图和控制器分离出来.就如同分层模式一样,我们以业务逻辑为中心,把表现层和数据访问层代码分离出来是一样的方法.框架只能在技术层 ...
- if switch练习(体重)
public class shencai { public static void main(String[] args) { int h= 186,g= 80; String Sex = " ...
- 自己写jstl标签解析long时间
数据库里存储的是long型的时间,现在想输出到jsp页面,由于使用的是jstl标签,而要显示的是可读的时间类型,找来找去有个fmt:formatDate可以转化,但是只能转date型,long型则不可 ...
- 要理解javascript中间apply和call
apply和call它是javascript一个非常重要的方法,.虽然与程序平时很少接触,但JS到处都在使用这个框架2方法. 2个方法是在Function.prototype中.也就是说每一个JS函数 ...
- Https协议简析及中间人攻击原理
1.基础知识 1.1 对称加密算法 对称加密算法的特点是加密密钥和解密密钥是同一把密钥K,且加解密速度快,典型的对称加密算法有DES.AES等 ...
- struts1面试题
由于找了很久的工作都没有找的,只能四处收集那个面试题的.和看面试题的 还有那个记忆力也不是很好了的,而那些公司面试的时候总会有一个面试题的! 在这里分享给大家(那个本来是想上传文件的,但是找不到的 ...
- Transition 1
W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...
- 进阶笔记(2)——JavaScript语言精碎
正则 / 正则表达式 / ^ 表示字符串开始 (?:...) 表示一个非捕获型分组(没多大意义) 后缀 ? 表示匹配 0 或 1次 ( ... ) 表示捕获型 ...