-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1172-dinner-plate-stacks.js
94 lines (81 loc) · 2.57 KB
/
1172-dinner-plate-stacks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* 1172. Dinner Plate Stacks
* https://leetcode.com/problems/dinner-plate-stacks/
* Difficulty: Hard
*
* 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
* capacity.
* - void push(int val) Pushes the given integer val into the leftmost stack with a 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 the stacks are empty.
* - int popAtStack(int index) Returns the value at the top of the stack with the given index
* index and removes it from that stack or returns -1 if the stack with that given index is empty.
*/
/**
* @param {number} capacity
*/
var DinnerPlates = function(capacity) {
this.maxCapacity = capacity;
this.plateStacks = [];
this.availableIndices = [];
};
/**
* @param {number} val
* @return {void}
*/
DinnerPlates.prototype.push = function(val) {
const targetIndex = this.availableIndices.length
? this.availableIndices.pop()
: this.plateStacks.length - 1;
if (!this.plateStacks[targetIndex] || this.plateStacks[targetIndex].length === this.maxCapacity) {
this.plateStacks.push([val]);
} else {
this.plateStacks[targetIndex].push(val);
}
};
/**
* @return {number}
*/
DinnerPlates.prototype.pop = function() {
while (this.plateStacks.length && !this.plateStacks.at(-1).length) {
while (this.plateStacks.length - 1 === this.availableIndices.at(-1)) {
this.availableIndices.pop();
}
this.plateStacks.pop();
}
if (!this.plateStacks.length) {
return -1;
}
const result = this.plateStacks.at(-1).pop() ?? -1;
while (this.plateStacks.length && !this.plateStacks.at(-1).length) {
while (this.plateStacks.length - 1 === this.availableIndices.at(-1)) {
this.availableIndices.pop();
}
this.plateStacks.pop();
}
return result;
};
/**
* @param {number} index
* @return {number}
*/
DinnerPlates.prototype.popAtStack = function(index) {
if (index >= this.plateStacks.length || !this.plateStacks[index].length) {
return -1;
}
const result = this.plateStacks[index].pop();
const temp = [];
while (this.availableIndices.length && this.availableIndices.at(-1) < index) {
temp.push(this.availableIndices.pop());
}
this.availableIndices.push(index);
while (temp.length) {
this.availableIndices.push(temp.shift());
}
return result;
};