forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1172.js
128 lines (112 loc) · 2.98 KB
/
1172.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const t = 0;
const parent = i => ((i + 1) >>> 1) - 1;
const left = i => (i << 1) + 1;
const right = i => (i + 1) << 1;
class PriorityQueue {
constructor(comparator = (a, b) => a > b) {
this._heap = [];
this._comparator = comparator;
}
size() {
return this._heap.length;
}
isEmpty() {
return this.size() == 0;
}
peek() {
return this._heap[t];
}
push(...values) {
values.forEach(value => {
this._heap.push(value);
this._siftUp();
});
return this.size();
}
pop() {
const poppedValue = this.peek();
const bottom = this.size() - 1;
if (bottom > t) {
this._swap(t, bottom);
}
this._heap.pop();
this._siftDown();
return poppedValue;
}
replace(value) {
const replacedValue = this.peek();
this._heap[t] = value;
this._siftDown();
return replacedValue;
}
_greater(i, j) {
return this._comparator(this._heap[i], this._heap[j]);
}
_swap(i, j) {
[this._heap[i], this._heap[j]] = [this._heap[j], this._heap[i]];
}
_siftUp() {
let node = this.size() - 1;
while (node > t && this._greater(node, parent(node))) {
this._swap(node, parent(node));
node = parent(node);
}
}
_siftDown() {
let node = t;
while (
(left(node) < this.size() && this._greater(left(node), node)) ||
(right(node) < this.size() && this._greater(right(node), node))
) {
let maxChild = (right(node) < this.size() && this._greater(right(node), left(node))) ? right(node) : left(node);
this._swap(node, maxChild);
node = maxChild;
}
}
}
/**
* @param {number} capacity
*/
var DinnerPlates = function(capacity) {
this.q = new PriorityQueue((a, b) => a < b);
this.n = capacity;
this.stacks = [];
this.stacks_len = 0;
};
/**
* @param {number} val
* @return {void}
*/
DinnerPlates.prototype.push = function(val) {
while (!this.q.isEmpty() && this.q.peek() < this.stacks_len && this.stacks[this.q.peek()].length == this.n) {
this.q.pop();
}
if (this.q.isEmpty()) this.q.push(this.stacks_len);
if (this.q.peek() == this.stacks_len) {
this.stacks.push(new Array());
this.stacks_len++;
}
this.stacks[this.q.peek()].push(val);
};
/**
* @return {number}
*/
DinnerPlates.prototype.pop = function() {
while (this.stacks_len > 0 && this.stacks[this.stacks_len - 1].length == 0) {
this.stacks.pop();
this.stacks_len--;
}
return this.popAtStack(this.stacks_len - 1);
};
/**
* @param {number} index
* @return {number}
*/
DinnerPlates.prototype.popAtStack = function(index) {
if (index >= 0 && index < this.stacks_len && this.stacks[index].length > 0) {
let ret = this.stacks[index].pop();
this.q.push(index);
return ret;
}
return -1;
};