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 解答的更多相关文章

  1. [LintCode] Min Stack 最小栈

    Implement a stack with min() function, which will return the smallest number in the stack. It should ...

  2. [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 ...

  3. leetcode 155. Min Stack --------- java

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

  4. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

  5. Min Stack

    Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...

  6. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  7. 155. Min Stack

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

  8. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  9. LeetCode算法题-Min Stack(Java实现)

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

随机推荐

  1. Tomcat部署项目通过—IP地址:端口访问

    如题所示,实现效果图如下: 设置如下: (1)修改${tomcat}/config/sever.xml文件虚拟内容目录: <Engine name="Catalina" de ...

  2. Linux shell编程02 shell程序的执行 及文件权限

    第一个shell脚本 1.       shell编程的方式 交互式shell编程 非交互式shell编程:执行的语句存放到一个文件 shell脚本:可以任意文件名,建议扩展名为sh 2.       ...

  3. poj 3190 Stall Reservations 贪心 + 优先队列

    题意:给定N头奶牛,每头牛有固定的时间[a,b]让农夫去挤牛奶,农夫也只能在对应区间对指定奶牛进行挤奶, 求最少要多少个奶牛棚,使得在每个棚内的奶牛的挤奶时间不冲突. 思路:1.第一个想法就是贪心,对 ...

  4. Android --- 字符串\n的换行问题

    我的一段文字"测试文本\n测试文本\n测试文本\n测试文本\n测试文本\n测试文本\n". 该段文字放在一个txt文本内. 我用流读取后,显示在一个TextView里. 我期望的显 ...

  5. 【转】DM8168图像处理Link

    1> dei dei 主要做数据交错处理,带缩放 dei control data flow: 2> sclr 8168中支持缩放按比例的分子和分母,只支持缩小,貌似不支持放大,且注意输出 ...

  6. Ajax 获取数据代码

    无刷新获取字符串: Html网页中: <script> //定义异步对象 var xmlHttp; //封装方法 function CreateXMLHTTP() { try { xmlH ...

  7. pyqt treeview基础学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import  * from Py ...

  8. iOS 3DES加密 和 java 3DES 解密

    首先进入头文件: #import <CommonCrypto/CommonDigest.h> #import <CommonCrypto/CommonCryptor.h> #i ...

  9. SpringMVC(三)——其他知识

    这篇博客,看一下在Controller类中,进行结果的跳转方式,对于SpringMVC框架中异常,如何统一捕捉,还有就是S(SpringMVC)SH的整合. 一,框架默认情况下是通过转发进行跳转的,如 ...

  10. c# 调用 友盟api

    今天要使用友盟的推送API来给我的app进行推送信息,调试了好久,老是返回500错误,最终在友盟的技术人员支持下完成了此操作,在此多谢友盟技术和客服人员. 把发方法和注意事项贴出来供大家参考. pub ...