-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path10828.js
More file actions
34 lines (31 loc) · 740 Bytes
/
10828.js
File metadata and controls
34 lines (31 loc) · 740 Bytes
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
const INPUT_FILE = process.platform === 'linux' ? '/dev/stdin' : './input';
const [, ...commands] = require('fs')
.readFileSync(INPUT_FILE)
.toString()
.trim()
.split('\n')
.map((line) => line.split(' '));
const stack = [];
const sol = [];
commands.forEach(([command, value]) => {
switch (command) {
case 'push':
stack.push(value);
break;
case 'pop':
if (stack.length) sol.push(stack.pop());
else sol.push(-1);
break;
case 'size':
sol.push(stack.length);
break;
case 'empty':
sol.push(stack.length ? 0 : 1);
break;
case 'top':
sol.push(stack.length ? stack[stack.length - 1] : -1);
break;
default:
}
});
console.log(sol.join('\n'));