forked from descire/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution1.js
47 lines (44 loc) · 814 Bytes
/
solution1.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
/**
* https://leetcode-cn.com/problems/design-a-stack-with-increment-operation/
*
* 5357. 设计一个支持增量操作的栈
*
* Medium
*
* 196ms 100.00%
* 41.4mb 100.00%
*/
var CustomStack = function(maxSize) {
this.maxSize = maxSize;
this.ans = [];
};
/**
* @param {number} x
* @return {void}
*/
CustomStack.prototype.push = function(x) {
if (this.ans.length < this.maxSize) {
this.ans.push(x);
}
};
/**
* @return {number}
*/
CustomStack.prototype.pop = function() {
if (this.ans.length !== 0) {
return this.ans.pop();
}
return -1;
};
/**
* @param {number} k
* @param {number} val
* @return {void}
*/
CustomStack.prototype.increment = function(k, val) {
for (let i = 0; i < k; i++) {
if (this.ans[i] !== undefined) {
this.ans[i] += val;
}
}
};