【LeetCode】Min Stack 解题报告

来源:互联网 发布:六级词汇书推荐知乎 编辑:程序博客网 时间:2024/04/29 05:16

http://blog.csdn.net/ljiabin/article/details/40982153


Hints:

  • Consider space-time tradeoff. How would you keep track of the minimums using extra space?
  • Make sure to consider duplicate elements.

O(n) runtime, O(n) space – Extra stack:

Use an extra stack to keep track of the current minimum value. During the push operation we choose the new element or the current minimum, whichever that is smaller to push onto the min stack.

O(n) runtime, O(n) space – Minor space optimization:

If a new element is larger than the current minimum, we do not need to push it on to the min stack. When we perform the pop operation, check if the popped element is the same as the current minimum. If it is, pop it off the min stack too.


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.
【用Java内置的Stack实现】

来自:https://oj.leetcode.com/discuss/15659/simple-java-solution-using-two-build-in-stacks?state=edit-15691&show=15659#q15659

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class MinStack {  
  2.     // stack: store the stack numbers  
  3.     private Stack<Integer> stack = new Stack<Integer>();  
  4.     // minStack: store the current min values  
  5.     private Stack<Integer> minStack = new Stack<Integer>();  
  6.   
  7.     public void push(int x) {  
  8.         // store current min value into minStack  
  9.         if (minStack.isEmpty() || x <= minStack.peek())  
  10.             minStack.push(x);  
  11.         stack.push(x);  
  12.     }  
  13.   
  14.     public void pop() {  
  15.         // use equals to compare the value of two object, if equal, pop both of them  
  16.         if (stack.peek().equals(minStack.peek()))  
  17.             minStack.pop();  
  18.         stack.pop();  
  19.     }  
  20.   
  21.     public int top() {  
  22.         return stack.peek();  
  23.     }  
  24.   
  25.     public int getMin() {  
  26.         return minStack.peek();  
  27.     }  
  28. }  

【分析】

这道题的关键之处就在于 minStack 的设计,push() pop() top() 这些操作Java内置的Stack都有,不必多说。

我最初想着再弄两个数组,分别记录每个元素的前一个比它大的和后一个比它小的,想复杂了。

第一次看上面的代码,还觉得它有问题,为啥只在 x<minStack.peek() 时压栈?如果,push(5), push(1), push(3) 这样minStack里不就只有5和1,这样pop()出1后, getMin() 不就得到5而不是3吗?其实这样想是错的,因为要想pop()出1之前,3就已经被pop()出了。. 

minStack 记录的永远是当前所有元素中最小的,无论 minStack.peek() 在stack 中所处的位置。


【不用内置Stack的实现】

来自:https://oj.leetcode.com/discuss/15651/my-java-solution-without-build-in-stack

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class MinStack {  
  2.     Node top = null;  
  3.   
  4.     public void push(int x) {  
  5.         if (top == null) {  
  6.             top = new Node(x);  
  7.             top.min = x;  
  8.         } else {  
  9.             Node temp = new Node(x);  
  10.             temp.next = top;  
  11.             top = temp;  
  12.             top.min = Math.min(top.next.min, x);  
  13.         }  
  14.     }  
  15.   
  16.     public void pop() {  
  17.         top = top.next;  
  18.         return;  
  19.     }  
  20.   
  21.     public int top() {  
  22.         return top == null ? 0 : top.val;  
  23.     }  
  24.   
  25.     public int getMin() {  
  26.         return top == null ? 0 : top.min;  
  27.     }  
  28. }  
  29.   
  30. class Node {  
  31.     int val;  
  32.     int min;  
  33.     Node next;  
  34.   
  35.     public Node(int val) {  
  36.         this.val = val;  
  37.     }  


0 0
原创粉丝点击