|
| 1 | +/** |
| 2 | + * 1172. Dinner Plate Stacks |
| 3 | + * https://leetcode.com/problems/dinner-plate-stacks/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You have an infinite number of stacks arranged in a row and numbered (left to right) from 0, |
| 7 | + * each of the stacks has the same maximum capacity. |
| 8 | + * |
| 9 | + * Implement the DinnerPlates class: |
| 10 | + * - DinnerPlates(int capacity) Initializes the object with the maximum capacity of the stacks |
| 11 | + * capacity. |
| 12 | + * - void push(int val) Pushes the given integer val into the leftmost stack with a size less |
| 13 | + * than capacity. |
| 14 | + * - int pop() Returns the value at the top of the rightmost non-empty stack and removes it |
| 15 | + * from that stack, and returns -1 if all the stacks are empty. |
| 16 | + * - int popAtStack(int index) Returns the value at the top of the stack with the given index |
| 17 | + * index and removes it from that stack or returns -1 if the stack with that given index is empty. |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {number} capacity |
| 22 | + */ |
| 23 | +var DinnerPlates = function(capacity) { |
| 24 | + this.maxCapacity = capacity; |
| 25 | + this.plateStacks = []; |
| 26 | + this.availableIndices = []; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * @param {number} val |
| 31 | + * @return {void} |
| 32 | + */ |
| 33 | +DinnerPlates.prototype.push = function(val) { |
| 34 | + const targetIndex = this.availableIndices.length |
| 35 | + ? this.availableIndices.pop() |
| 36 | + : this.plateStacks.length - 1; |
| 37 | + |
| 38 | + if (!this.plateStacks[targetIndex] || this.plateStacks[targetIndex].length === this.maxCapacity) { |
| 39 | + this.plateStacks.push([val]); |
| 40 | + } else { |
| 41 | + this.plateStacks[targetIndex].push(val); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * @return {number} |
| 47 | + */ |
| 48 | +DinnerPlates.prototype.pop = function() { |
| 49 | + while (this.plateStacks.length && !this.plateStacks.at(-1).length) { |
| 50 | + while (this.plateStacks.length - 1 === this.availableIndices.at(-1)) { |
| 51 | + this.availableIndices.pop(); |
| 52 | + } |
| 53 | + this.plateStacks.pop(); |
| 54 | + } |
| 55 | + |
| 56 | + if (!this.plateStacks.length) { |
| 57 | + return -1; |
| 58 | + } |
| 59 | + |
| 60 | + const result = this.plateStacks.at(-1).pop() ?? -1; |
| 61 | + |
| 62 | + while (this.plateStacks.length && !this.plateStacks.at(-1).length) { |
| 63 | + while (this.plateStacks.length - 1 === this.availableIndices.at(-1)) { |
| 64 | + this.availableIndices.pop(); |
| 65 | + } |
| 66 | + this.plateStacks.pop(); |
| 67 | + } |
| 68 | + |
| 69 | + return result; |
| 70 | +}; |
| 71 | + |
| 72 | +/** |
| 73 | + * @param {number} index |
| 74 | + * @return {number} |
| 75 | + */ |
| 76 | +DinnerPlates.prototype.popAtStack = function(index) { |
| 77 | + if (index >= this.plateStacks.length || !this.plateStacks[index].length) { |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + const result = this.plateStacks[index].pop(); |
| 82 | + const temp = []; |
| 83 | + |
| 84 | + while (this.availableIndices.length && this.availableIndices.at(-1) < index) { |
| 85 | + temp.push(this.availableIndices.pop()); |
| 86 | + } |
| 87 | + |
| 88 | + this.availableIndices.push(index); |
| 89 | + while (temp.length) { |
| 90 | + this.availableIndices.push(temp.shift()); |
| 91 | + } |
| 92 | + |
| 93 | + return result; |
| 94 | +}; |
0 commit comments