-
Notifications
You must be signed in to change notification settings - Fork 481
/
1381.js
42 lines (39 loc) · 789 Bytes
/
1381.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
/**
* @param {number} maxSize
*/
var CustomStack = function(maxSize) {
this.c = maxSize;
this.s = [];
this.inc = [];
};
/**
* @param {number} x
* @return {void}
*/
CustomStack.prototype.push = function(x) {
if (this.s.length < this.c) {
this.s.push(x);
this.inc.push(0);
}
};
/**
* @return {number}
*/
CustomStack.prototype.pop = function() {
if (this.s.length == 0) return -1;
let i = this.inc.length - 1;
if (i > 0) {
this.inc[i - 1] += this.inc[i];
}
return this.s.pop() + this.inc.pop();
};
/**
* @param {number} k
* @param {number} val
* @return {void}
*/
CustomStack.prototype.increment = function(k, val) {
if (this.inc.length) {
this.inc[Math.min(k, this.inc.length) - 1] += val;
}
};