LeetCode 716. Max Stack

原题链接在这里:https://leetcode.com/problems/max-stack/description/

题目:

Design a max stack that supports push, pop, top, peekMax and popMax.

  1. push(x) -- Push element x onto stack.
  2. pop() -- Remove the element on top of the stack and return it.
  3. top() -- Get the element on the top.
  4. peekMax() -- Retrieve the maximum element in the stack.
  5. popMax() -- Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.

Example 1:

MaxStack stack = new MaxStack();
stack.push(5); 
stack.push(1);
stack.push(5);
stack.top(); -> 5
stack.popMax(); -> 5
stack.top(); -> 1
stack.peekMax(); -> 5
stack.pop(); -> 1
stack.top(); -> 5

Note:

  1. -1e7 <= x <= 1e7
  2. Number of operations won't exceed 10000.
  3. The last four operations won't be called when stack is empty.

题解:

两个stack来解决. 不同的是popMax, 用temp stack 来保存找到最大值之前的, pop出max后再加回去, 同时更新maxStk.

Note: when popMax, get temp back, it needs to update maxStk as well.

Time Complexity: push, O(1). pop, O(1). top, O(1). peekMax, O(1). popMax, O(n). n是stack中数字的个数.

Space: O(n).

AC Java:

 1 class MaxStack {
 2     Stack<Integer> stk;
 3     Stack<Integer> maxStk;
 4     
 5     /** initialize your data structure here. */
 6     public MaxStack() {
 7         stk = new Stack<Integer>();
 8         maxStk = new Stack<Integer>();
 9     }
10     
11     public void push(int x) {
12         stk.push(x);
13         if(maxStk.isEmpty() || maxStk.peek()<=x){
14             maxStk.push(x);
15         }
16     }
17     
18     public int pop() {
19         int x = stk.pop();
20         if(!maxStk.isEmpty() && x==maxStk.peek()){
21             maxStk.pop();
22         }
23 
24         return x;
25     }
26     
27     public int top() {
28         return stk.peek();
29     }
30     
31     public int peekMax() {
32         return maxStk.peek();
33     }
34     
35     public int popMax() {
36         Stack<Integer> tempStk = new Stack<Integer>();
37         int x = maxStk.pop();
38         while(!stk.isEmpty() && stk.peek()<x){
39             tempStk.push(stk.pop());
40         }
41         
42         stk.pop();
43         while(!tempStk.isEmpty()){
44             int top = tempStk.pop();
45             push(top);
46         }
47         return x;       
48     }
49 }
50 
51 /**
52  * Your MaxStack object will be instantiated and called as such:
53  * MaxStack obj = new MaxStack();
54  * obj.push(x);
55  * int param_2 = obj.pop();
56  * int param_3 = obj.top();
57  * int param_4 = obj.peekMax();
58  * int param_5 = obj.popMax();
59  */

To decrease time complexity of popMax, we can use a way like lazy pointer.
Have a set to maintain the index of removed elements. Also use maxHeap to maintain the max {index, val}.

When pop, first pop all the removed elements in the stack. Then add the latest popped top's index into set.

When popMax, pop all the removed eleemnts in the maxHeap, then add the latest poped top's index into set.

Time Complexity: push, O(logn). pop(), O(k). top, O(k). peekMax, O(k). popMax, O(k + logn). n = maxHeap.size(). k = removed.size().

Space: O(m + n + k). m = stk.size().

AC Java:

 1 class MaxStack {
 2     Stack<int[]> stk;
 3     PriorityQueue<int[]> maxHeap;
 4     int ind;
 5     Set<Integer> removed;
 6 
 7     public MaxStack() {
 8         stk = new Stack<>();
 9         maxHeap = new PriorityQueue<>((a, b) -> a[1] == b[1] ? b[0] - a[0] : b[1] - a[1]);
10         ind = 0;
11         removed = new HashSet<>();
12     }
13     
14     public void push(int x) {
15         stk.push(new int[]{ind, x});
16         maxHeap.add(new int[]{ind, x});
17         ind++;
18     }
19     
20     public int pop() {
21         while(removed.contains(stk.peek()[0])){
22             stk.pop();
23         }
24 
25         int[] top = stk.pop();
26         removed.add(top[0]);
27         return top[1];
28     }
29     
30     public int top() {
31         while(removed.contains(stk.peek()[0])){
32             stk.pop();
33         }
34 
35         return stk.peek()[1];
36     }
37     
38     public int peekMax() {
39         while(removed.contains(maxHeap.peek()[0])){
40             maxHeap.poll();
41         }
42 
43         return maxHeap.peek()[1];
44     }
45     
46     public int popMax() {
47         while(removed.contains(maxHeap.peek()[0])){
48             maxHeap.poll();
49         }
50 
51         int[] top = maxHeap.poll();
52         removed.add(top[0]);
53         return top[1];
54     }
55 }
56 
57 /**
58  * Your MaxStack object will be instantiated and called as such:
59  * MaxStack obj = new MaxStack();
60  * obj.push(x);
61  * int param_2 = obj.pop();
62  * int param_3 = obj.top();
63  * int param_4 = obj.peekMax();
64  * int param_5 = obj.popMax();
65  */

类似Min Stack.

posted @ 2017-11-21 09:35  Dylan_Java_NYC  阅读(759)  评论(0编辑  收藏  举报