-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsolution.js
33 lines (30 loc) · 861 Bytes
/
solution.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
var CustomStack = function (maxSize) {
this.maxSize = maxSize;
this.stack = [];
this.inc = [];
};
CustomStack.prototype.push = function (x) {
if (this.stack.length < this.maxSize) {
this.stack.push(x);
this.inc.push(0); // Initialize increment for this element
}
};
CustomStack.prototype.pop = function () {
if (this.stack.length === 0) {
return -1;
}
let idx = this.stack.length - 1;
let result = this.stack[idx] + this.inc[idx]; // Apply any pending increments
if (idx > 0) {
this.inc[idx - 1] += this.inc[idx]; // Propagate increment to the next element
}
this.stack.pop();
this.inc.pop();
return result;
};
CustomStack.prototype.increment = function (k, val) {
let limit = Math.min(k, this.stack.length) - 1;
if (limit >= 0) {
this.inc[limit] += val; // Add increment to the bottom k-th element
}
};