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

https://www.acmicpc.net/problem/1620

 

1620번: 나는야 포켓몬 마스터 이다솜

첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");
const [N, M] = input[0].split(" ").map(d => Number(d));
const dic = {};
const dic1 = {};

for(let i = 1; i <= N ; i++) {
    dic[i] = input[i];
    dic1[input[i]] = i;
}

for(let i = N + 1 ; i < input.length ; i++) {
    if(Number.isNaN(Number(input[i]))) {
        console.log(dic1[input[i]]);
    } else {
        console.log(dic[input[i]]);
    }
}
728x90

https://www.acmicpc.net/problem/10816

 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().split("\n");
const dic = {};
const N = Number(input[0]);
const M = Number(input[2]);
const sang = input[1].split(" ");
const comp = input[3].split(" ");
const answer = [];


sang.forEach(d => {
    if(dic[d] === undefined) dic[d] = 1;
    else dic[d] ++;
});

comp.forEach((d) => {
    if(dic[d] !== undefined ) answer.push(dic[d]);
    else answer.push(0);
})

console.log(answer.join(" "));
728x90

https://www.acmicpc.net/problem/10815

 

10815번: 숫자 카드

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().split("\n");
const dic = {};
const N = Number(input[0]);
const M = Number(input[2]);
const sang = input[1].split(" ");
const comp = input[3].split(" ");
const answer = [];


sang.forEach(c => {
    dic[c] = 0;
});

comp.forEach((d) => {
    if(dic[d] !== undefined ) answer.push(1);
    else answer.push(0);
})

console.log(answer.join(" "));
728x90

+ Recent posts