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"));
'Algorithm&CodingTest > Baekjoon' 카테고리의 다른 글
[Beakjoon] [1427] 정렬 - 소트인사이드 (0) | 2023.02.24 |
---|---|
[Baekjoon] [11650] 실버5 - 좌표 정렬하기 NodeJs (0) | 2023.02.24 |
[Baekjoon] [10845] 큐/스택 - 큐 (0) | 2023.02.23 |
[Baekjoon] [9012] 스택 - 괄호 (0) | 2023.02.23 |
[Baekjoon] [1620] 집합과 맵 - 나는야 포켓몬 마스터 이다솜 (0) | 2023.02.23 |