-
Notifications
You must be signed in to change notification settings - Fork 0
/
10828.js
38 lines (31 loc) · 850 Bytes
/
10828.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
const { off } = require('process');
const readline = require('readline');
const { callbackify } = require('util');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const data = [];
rl.on('line', (input) => {
data.push(input);
}).on('close', () => {
let N = Number(data.shift());
let stack = [];
let order = data;
const calc = {
pop: () => stack.pop() || -1,
size: () => stack.length,
empty: () => (stack[0] ? 0 : 1),
top: () => stack[stack.length - 1] || -1,
push: (item) => {
stack.push(item.split(' ')[1]);
return '';
},
};
const result = order.reduce(
(acc, v) => acc + (calc[v] ? `${calc[v]()}\n` : calc.push(v)),
''
);
console.log(result);
process.exit();
});