-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path155-min-stack.js
More file actions
81 lines (72 loc) · 1.57 KB
/
Copy path155-min-stack.js
File metadata and controls
81 lines (72 loc) · 1.57 KB
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
var MinStack = function () {
this.values = [];
this.min = [];
};
/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function (val) {
this.min.push(Math.min(val, this.min[this.min.length - 1] ?? val));
this.values.push(val);
};
/**
* @return {void}
*/
MinStack.prototype.pop = function () {
this.min.pop();
this.values.pop();
};
/**
* @return {number}
*/
MinStack.prototype.top = function () {
return this.values[this.values.length - 1];
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function () {
return this.min[this.min.length - 1];
};
const data = [{
ops: ["MinStack", "push", "push", "push", "getMin", "pop", "top", "getMin"],
args: [null, -2, 0, -3, null, null, null, null],
output: [null, null, null, null, -3, null, 0, -2]
},
{
ops: ["MinStack","push","getMin"],
args: [null, -3, null],
output: [null,null,-3]
},
];
for (let d of data) {
console.log(JSON.stringify(d));
const result = [];
let stack;
for (let i = 0; i < d.ops.length; i++) {
const operation = d.ops[i];
let r = null;
switch (operation) {
case "MinStack":
stack = new MinStack();
break;
case "push":
stack.push(d.args[i]);
break;
case "getMin":
r = stack.getMin();
break;
case "pop":
stack.pop();
break;
case "top":
r = stack.top();
break;
}
result.push(r);
}
console.log('result = ', result);
(JSON.stringify(result) === JSON.stringify(d.output)) ? console.log('ok') : console.error('nok');
console.log('----------');
}