【leetcode】1172. Dinner Plate Stacks

Topics are as follows:

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.

Problem-solving ideas: This title I spent three list, one is stack_list, so save the stack information; one is nonEmptyStack, the current record is not empty subscript stack; it also is a availableStack, less than the current record of the stack mark. When the push, only need to find the minimum value of all subscripts availableStack, the stack can be inserted into the corresponding, if availableStack is empty, a new stack, inserting stack_list, and update the status availableStack and nonEmptyStack; POP operation is to find in the case of maximum nonEmptyStack, do their corresponding stack pop operation, and popAsStack more simple operation, can be directly accessed by the subscript stack_list. Note that each of the stack there is any action to be synchronized and updated availableStack nonEmptyStack. Because it is seeking the maximum or minimum availableStack and nonEmptyStack only need to keep availableStack and nonEmptyStack elements in order to update availableStack and nonEmptyStack you can use binary search.

code show as below:

class DinnerPlates(object):

    def __init__(self, capacity):
        """
        :type capacity: int
        """
        self.stack_list = []
        self.availableStack = []
        self.capacity = capacity
        self.nonEmptyStack = []

    def push(self, val):
        """
        :type val: int
        :rtype: None
        """
        if len(self.availableStack) == 0:
            inx = len(self.stack_list)
            #self.availableStack.append(len(self.stack_list))
            self.stack_list.append([val])
            if len(self.stack_list[inx]) < self.capacity:
                self.availableStack.append(inx)
        else:
            inx = self.availableStack[0]
            self.stack_list[inx].append(val)
            if len(self.stack_list[inx]) >= self.capacity:
                self.availableStack.pop(0)
        import bisect
        b_inx = bisect.bisect_left(self.nonEmptyStack,inx)
        if b_inx == -1 or b_inx == len(self.nonEmptyStack) or self.nonEmptyStack[b_inx] != inx:
            bisect.insort_left(self.nonEmptyStack,inx)


    def pop(self):
        """
        :rtype: int
        """
        if len(self.nonEmptyStack) == 0:
            return -1
        inx = self.nonEmptyStack[-1]
        v = self.stack_list[inx].pop(-1)
        if len(self.stack_list[inx]) == 0:
            self.nonEmptyStack.pop(-1)
        return v


    def popAtStack(self, index):
        """
        :type index: int
        :rtype: int
        """
        import bisect
        b_inx = bisect.bisect_left(self.nonEmptyStack, index)
        if b_inx == -1 or b_inx == len(self.nonEmptyStack) or self.nonEmptyStack[b_inx] != index:
            return -1
        v = self.stack_list[index].pop(-1)
        if len(self.stack_list[index]) == 0:
            del self.nonEmptyStack[b_inx]

        b_inx = bisect.bisect_left(self.availableStack, index)
        if b_inx == -1 or b_inx == len(self.availableStack) or self.availableStack[b_inx] != index:
            bisect.insort_left(self.availableStack,index)

        return v

 

Guess you like

Origin www.cnblogs.com/seyjs/p/11520402.html