https://www.acmicpc.net/problem/10845
10845번: 큐
첫째 줄에 주어지는 명령의 수 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.shift());
const queue = [];
const answer = [];
for(let i = 0 ; i < N ; i++) {
const [command, value] = input[i].split(" ");
if(command === 'push') {
queue.push(Number(value));
} else if( command === 'pop') {
if(queue.length === 0 ) answer.push(-1);
else answer.push(queue.shift());
} else if(command === 'empty') {
if(queue.length === 0 ) answer.push(1);
else answer.push(0);
} else if(command === 'size') {
answer.push(queue.length);
} else if(command === 'front') {
if(queue.length === 0 ) answer.push(-1);
else answer.push(queue[0]);
} else if(command === 'back') {
if(queue.length === 0 ) answer.push(-1);
else answer.push(queue[queue.length-1]);
}
}
console.log(answer.join('\n'));
처음에 제출한 거 시간초과 걸렸는데 매번 console 찍어줘서 시간초과 난 거였음.
answer 배열에 모두 넣은 후 한 번에 출력하는 방식으로 바꿔서 통과됨.
728x90
'Algorithm&CodingTest > Baekjoon' 카테고리의 다른 글
[Baekjoon] [11650] 실버5 - 좌표 정렬하기 NodeJs (0) | 2023.02.24 |
---|---|
[Baekjoon] [10828] 스택/큐 - 스택 (0) | 2023.02.24 |
[Baekjoon] [9012] 스택 - 괄호 (0) | 2023.02.23 |
[Baekjoon] [1620] 집합과 맵 - 나는야 포켓몬 마스터 이다솜 (0) | 2023.02.23 |
[Baekjoon] [1764] 집합과 맵 - 듣보잡 (0) | 2023.02.23 |