1172. tray stack

1172. tray stack

We ∞ unlimited number of stacks in a row, from left to right order numbered from 0. Each stack the maximum capacity of the same capacity.

DinnerPlates implement a class called "dish" in:

DinnerPlates (int capacity) - gives the maximum stack capacity capacity.
void push (int val) - val which will be given a positive integer from left to right is pushed into the first stack is not full.
int pop () - returns from right to left top of the stack first non-null value, and remove it from the stack; if all the stacks are empty, return -1.
int popAtStack (int index) - Returns the value of the index number of the top stack and remove it from the stack; index number if the stack is empty, return -1.
 

Example:

输入:
["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],[],[],[],[],[]]
输出:
[null,null,null,null,null,null,2,null,null,20,21,5,4,3,1,-1]

Explain:
DinnerPlates DinnerPlates = D (2); // initialization, the stack 2 = maximum capacity Capacity
D.push (. 1);
D.push (2);
D.push (. 3);
D.push (. 4);
D. push (5); // stack status is:. 4 2
  . 1. 3. 5
﹈ ﹈ ﹈
D.popAtStack (0); // returns 2. Stack status is: 4
  . 1. 3. 5
﹈ ﹈ ﹈
D.push (20); // stack status is: 204
  . 1. 3. 5
﹈ ﹈ ﹈
D.push (21); // stack status is: 204 21 is
  . 1. 3. 5
﹈ ﹈ ﹈
D.popAtStack (0); // returns 20. Situation stack of: 21. 4
  . 1. 3. 5
﹈ ﹈ ﹈
D.popAtStack (2); // returns 21. Status stack is: 4
  1 3 5
﹈ ﹈ ﹈
D.pop () // returns 5. Status stack is: 4
  1 3
﹈ ﹈
D.pop () // returns 4. Status stack is: 13
﹈ ﹈
D.pop () // returns 3. Status stack: 1

D.pop () // returns 1. Now do not stack.
D.pop () // returns -1. Still do not stack.
 

prompt:

1 <= Capacity <= 20000
1 <= Val <= 20000
0 <= index <= 100000
most will push, pop, and popAtStack be 200000 times call.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/dinner-plate-stacks
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/think90/p/11440158.html