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和在恒定时间内检索最小 ...
随机推荐
- JS--回到顶部代码
原文地址:http://www.cnblogs.com/liguiqiang1986/articles/3132023.html JS--回到顶部代码 <!DOCTYPE html PUBLIC ...
- Php基本语法数据类型操作基础训练
<?php /* * Created on 2015年12月17日 * * To change the template for this generated file go to * Wind ...
- Find the Duplicate Number 解答
Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...
- sae-v2ex 一个运行在SAE上的类似v2ex的轻型python论坛 - 技术讨论 - 云计算开发者社区 - Powered by Discuz!
sae-v2ex 一个运行在SAE上的类似v2ex的轻型python论坛 - 技术讨论 - 云计算开发者社区 - Powered by Discuz! sae-v2ex 一个运行在SAE上的类似v2e ...
- Linux Top 命令
TOP命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况. TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止 ...
- python基础学习05(核心编程第二版)部分
# -*- coding: utf-8 -*- # ==================== #File: python #Author: python #Date: 2014 #========== ...
- [最新版]MJRefresh解析与详细使用指导
俗话说 "工欲善其事,必先利其器",好的成熟的第三方,是我们开发路上的利器:俗话又说"君子生非异也,善假于物也"NB的人并不是生下来就和别人不一样,只是他们擅于 ...
- SSO之CAS基础及应用视频教程(2)
CAS介绍 CAS = Central Authentication Service,中央认证服务.CAS 是 Yale 大学发起的一个开源项目,能够为 Web 应用系统或者非Web应用系统提供一种可 ...
- [HeadFrist-HTMLCSS学习笔记][第一章Web语言:开始了解HTML]
head title body 元素= 开始标记 + 内容 +结束标记 还能给段落一个变量名 <p id="houseblend"> body </p> s ...
- Android学习笔记--JNI的使用方法
1.JNI是什么 JNI是Java Native Interface的缩写,它提供若干的API实现Java与其他语言之间的通信.而Android Framework由基于Java语言的的Java层与基 ...