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:

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:


Solution:

class DinnerPlates {
    
    List<Deque<Integer>> stacks;
    PriorityQueue<Integer> pq;
    int cap;
    
    public DinnerPlates(int capacity) {
        stacks = new ArrayList();
        pq = new PriorityQueue();
        cap = capacity;
    }
    
    public void push(int val) {
        if (pq.isEmpty()) {
            stacks.add(new ArrayDeque());
            pq.offer(stacks.size() - 1);
        }
        int idx = Math.min(pq.poll(), stacks.size() - 1);
        stacks.get(idx).push(val);
        if (stacks.get(idx).size() < this.cap) {
            pq.offer(idx);
        }
    }
    
    public int pop() {
        // System.out.println("~~~~~~~~~~~~~~~~~~~");
        // System.out.println(stacks);
        int size = stacks.size();
        if (size == 0) return -1;
        int ret = -1;
        if (!stacks.get(size - 1).isEmpty()) {
            ret = stacks.get(size - 1).pop();
        }
        if (stacks.get(size - 1).isEmpty()) {
            while (stacks.size() > 0 && stacks.get(stacks.size() - 1).isEmpty()) {
                stacks.remove(stacks.size() - 1);
            }
        } else {
            pq.offer(size - 1);
        }
        // System.out.println(stacks);
        // System.out.println("~~~~~~~~~~~~~~~~~~~");
        return ret;
    }
    
    public int popAtStack(int index) {
        if (index >= stacks.size()) return -1;
        int ret = -1;
        if (!stacks.get(index).isEmpty()) {
            ret = stacks.get(index).pop();
        }
        if (stacks.get(index).isEmpty()) {
            if (index == stacks.size() - 1) {
                while (stacks.size() > 0 && stacks.get(stacks.size() - 1).isEmpty()) {
                    stacks.remove(stacks.size() - 1);
                }
            }
        } else {
            pq.offer(index);
        }
        return ret;
    }
}

/**
 * Your DinnerPlates object will be instantiated and called as such:
 * DinnerPlates obj = new DinnerPlates(capacity);
 * obj.push(val);
 * int param_2 = obj.pop();
 * int param_3 = obj.popAtStack(index);
 */