LeetCode 1172. Dinner Plate Stacks

原题链接在这里:https://leetcode.com/problems/dinner-plate-stacks/

题目:

You have an infinite number of stacks arranged in a row and numbered (left to right) from 0, each of the stacks has the same maximum capacity.

Implement the DinnerPlates class:

  • DinnerPlates(int capacity) Initializes the object with the maximum capacity of the stacks.
  • void push(int val) pushes the given positive integer val into the leftmost stack with size less than capacity.
  • int pop() returns the value at the top of the rightmost non-empty stack and removes it from that stack, and returns -1 if all stacks are empty.
  • int popAtStack(int index) returns the value at the top of the stack with the given index and removes it from that stack, and returns -1 if the stack with that given index is empty.

Example:

Input: 
["DinnerPlates","push","push","push","push","push","popAtStack","push","push","popAtStack","popAtStack","pop","pop","pop","pop","pop"]
[[2],[1],[2],[3],[4],[5],[0],[20],[21],[0],[2],[],[],[],[],[]]
Output: 
[null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1]

Explanation: 
DinnerPlates D = DinnerPlates(2);  // Initialize with capacity = 2
D.push(1);
D.push(2);
D.push(3);
D.push(4);
D.push(5);         // The stacks are now:  2  4
                                           1  3  5
                                           ﹈ ﹈ ﹈
D.popAtStack(0);   // Returns 2.  The stacks are now:     4
                                                       1  3  5
                                                       ﹈ ﹈ ﹈
D.push(20);        // The stacks are now: 20  4
                                           1  3  5
                                           ﹈ ﹈ ﹈
D.push(21);        // The stacks are now: 20  4 21
                                           1  3  5
                                           ﹈ ﹈ ﹈
D.popAtStack(0);   // Returns 20.  The stacks are now:     4 21
                                                        1  3  5
                                                        ﹈ ﹈ ﹈
D.popAtStack(2);   // Returns 21.  The stacks are now:     4
                                                        1  3  5
                                                        ﹈ ﹈ ﹈ 
D.pop()            // Returns 5.  The stacks are now:      4
                                                        1  3 
                                                        ﹈ ﹈  
D.pop()            // Returns 4.  The stacks are now:   1  3 
                                                        ﹈ ﹈   
D.pop()            // Returns 3.  The stacks are now:   1 
                                                        ﹈   
D.pop()            // Returns 1.  There are no stacks.
D.pop()            // Returns -1.  There are still no stacks.

 

Constraints:

  • 1 <= capacity <= 20000
  • 1 <= val <= 20000
  • 0 <= index <= 100000
  • At most 200000 calls will be made to pushpop, and popAtStack.

题解:

Have a minHeap to track previous pop at index position.

When pop at index, check if index is within list size and pointing stack is not empty, add this index to min heap.

When push, first check if min heap peek is already >= list size. If yes, that means the smallest index is already larger than list size. Those stack must be empty by pop. Clear the min heap.

Otherwise, check if min heap is empty, if not, add to the last stack, otherwise, add to that index.

When pop, already clear the empty stack from last and then pop the last non-empty stack.

Time Complexity: push, O(1). pop, amortize O(list.size()). popAtIndex O(logn). n = minHeap.size().

Space: O(m). m = list.size() * capacity.

AC Java: 

 1 class DinnerPlates {
 2     List<Stack<Integer>> list;
 3     PriorityQueue<Integer> minHeap;
 4     int capa;
 5     
 6     public DinnerPlates(int capacity) {
 7         list = new ArrayList<>();
 8         minHeap = new PriorityQueue<>();
 9         capa = capacity;
10     }
11     
12     public void push(int val) {
13         if(!minHeap.isEmpty() && minHeap.peek() >= list.size()){
14             minHeap.clear();
15         }
16         
17         if(minHeap.isEmpty()){
18             if(list.size() == 0 || list.get(list.size() - 1).size() == capa){
19                 list.add(new Stack<>());
20             }
21             
22             list.get(list.size() - 1).push(val);
23         }else{
24             list.get(minHeap.poll()).add(val);
25         }
26     }
27     
28     public int pop() {
29         while(list.size() > 0 && list.get(list.size() - 1).size() == 0){
30             list.remove(list.size() - 1);
31         }
32         
33         if(list.size() == 0){
34             return -1;
35         }
36         
37         return list.get(list.size() - 1).pop();
38     }
39     
40     public int popAtStack(int index) {
41         if(index >= list.size()){
42             return -1;
43         }
44         
45         if(list.get(index).size() == 0){
46             return -1;
47         }
48         
49         minHeap.add(index);
50         return list.get(index).pop();
51     }
52 }
53 
54 /**
55  * Your DinnerPlates object will be instantiated and called as such:
56  * DinnerPlates obj = new DinnerPlates(capacity);
57  * obj.push(val);
58  * int param_2 = obj.pop();
59  * int param_3 = obj.popAtStack(index);
60  */
posted @ 2020-01-14 13:54  Dylan_Java_NYC  阅读(401)  评论(0编辑  收藏  举报