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. 关于qt学习的一点小记录(2)

    嗯...这次接了个单 要求图形界面,刚好可以巩固并学习下QT.毫不犹豫的就接了 下面记录下出现的问题: 1. QWidget和QDialog QDialog下的槽函数有accept()与reject( ...

  2. qt Graphics View Framework(非重点)

    Graphics View 提供了一种接口,用于管理大量自定义的 2D 图形元素,并与之进行交互:还提供了用于将这些元素进行可视化显示的观察组件,并支持缩放和旋转. 说明;Graphics View ...

  3. GCD 延时操作

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_ ...

  4. WinForm 控件不闪烁

    1: [DllImport("user32")] 2: public static extern int SendMessage(IntPtr hwnd, int wMsg, in ...

  5. HashMap的使用方法及注意事项

    99.Map(映射):Map 的keySet()方法会返回 key 的集合,因为 Map 的键是不能重复的,因此 keySet()方法的返回类型是 Set:而 Map 的值是可以重复的,因此 valu ...

  6. chroot 与 jail

    所谓“监牢”就是指通过chroot机制来更改某个进程所能看到的根文件夹,即将某进程限制在指定文件夹中,保证该进程仅仅能对该文件夹及其子文件夹的文件有所动作,从而保证整个server的安全. 创建chr ...

  7. [Immutable + AngularJS] Use Immutable .List() for Angular array

    const stores = Immutable.List([ { name: 'Store42', position: { latitude: 61.45, longitude: 23.11, }, ...

  8. [HeadFist-HTMLCSS学习笔记][第五章认识媒体]

    图像格式 PNG 多种颜色透明 无损压缩 PNG-8,PNG-16,PNG-32 多用于logo GIF 动画 256色 无损 JPEG 不能透明 多用于照片 img URL能插入 alt属性 = 如 ...

  9. 在asp.net中导出表格Excel数据

    第一步:需要引用org.in2bits.MyXls程序集到使用页面 第二步:前台代码 <asp:Button ID="LeadingOut" runat="serv ...

  10. Android--------Java接口回调

    >###回调说明 回调说明:         1.class A,class B         2.class A 实现接口callback         3.class B拥有一个参数为c ...