Algorithm&CodingTest/Baekjoon
[Baekjoon] [10828] 스택/큐 - 스택
mellowg
2023. 2. 24. 12:15
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");
const N = Number(input[0]);
const stack = [];
const result = [];
for(let i = 0 ;i < N ; i++) {
const command = input[i+1].split(" ");
if(command[0] === 'push') {
stack.push(Number(command[1]));
} else if(command[0] === 'pop') {
result.push(stack.length === 0 ? -1 : stack.pop());
} else if(command[0] === 'size') {
result.push(stack.length);
} else if(command[0] === 'empty') {
result.push(stack.length === 0 ? 1 : 0);
} else if(command[0] === 'top'){
result.push(stack.length === 0 ? -1 : stack[stack.length-1]);
}
}
console.log(result.join("\n"));
728x90