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

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/9012

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split("\n");

const N = Number(input.shift());

for(let i = 0 ; i  < N ; i++) {
    const stack = [];
    let prev = '';
    if(input[i].length % 2 !== 0) console.log("NO");
    else {
        const arr = input[i].split("");
        arr.forEach((a, index) => {
           if(index === 0 && a === ')') {
               return false;
           } else {
               if(stack.length === 0) {
                   stack.push(a);
               } else {
                   prev = stack.pop();
                   if(!(prev === '(' && a === ')')) {
                       stack.push(prev);
                       stack.push(a);
                   }
               }
           }
        });
        if(stack.length === 0) console.log('YES');
        else console.log("NO")
    }
}
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

+ Recent posts