Design a Stack With Increment Operation LeetCode solution in JavaScript

1
var CustomStack = function (maxSize) {
2
this.stack = new Array(maxSize).fill(-1);
3
this.maxSize = maxSize;
4
this.size = 0;
5
};
6

7
CustomStack.prototype.push = function (x) {
8
if (this.size < this.maxSize) {
9
this.stack[this.size] = x;
10
this.size++;
11
}
12
};
13

14
CustomStack.prototype.pop = function () {
15
if (this.size > 0) {
16
this.size--;
17
return this.stack[this.size];
18
}
19
return -1;
20
};
21

22
CustomStack.prototype.increment = function (k, val) {
23
let count = k >= this.size ? this.size - 1 : k - 1;
24

25
while (count >= 0) {
26
this.stack[count] += val;
27
count--;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0