let's get IT with DAVINA ๐Ÿ’ป

[๋ฐฑ์ค€ #10845] ํ ๋ณธ๋ฌธ

DEV_IN/๋ฌธ์ œํ’€์ด

[๋ฐฑ์ค€ #10845] ํ

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 3. 17. 17:08

โ“ ๋ฌธ์ œ

์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ํ๋ฅผ ๊ตฌํ˜„ํ•œ ๋‹ค์Œ, ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๋ช…๋ น์€ ์ด ์—ฌ์„ฏ ๊ฐ€์ง€์ด๋‹ค.

  • push X: ์ •์ˆ˜ X๋ฅผ ํ์— ๋„ฃ๋Š” ์—ฐ์‚ฐ์ด๋‹ค.
  • pop: ํ์—์„œ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ๋นผ๊ณ , ๊ทธ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ํ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • size: ํ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • empty: ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • front: ํ์˜ ๊ฐ€์žฅ ์•ž์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ํ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • back: ํ์˜ ๊ฐ€์žฅ ๋’ค์— ์žˆ๋Š” ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ ํ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ—๏ธ ์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€ ์•Š์€ ๋ช…๋ น์ด ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

๐Ÿ—๏ธ ์ถœ๋ ฅ

์ถœ๋ ฅํ•ด์•ผํ•˜๋Š” ๋ช…๋ น์ด ์ฃผ์–ด์งˆ ๋•Œ๋งˆ๋‹ค, ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ—๏ธ ์˜ˆ์ œ ์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ


โ—๏ธ ๋น„๋‹ˆ's ํ’€์ด

stack ์—ฐ์Šต๋ฌธ์ œ๋Œ€๋กœ๋งŒ ๋กœ์ง์„ ๋”ฐ๋ผ๊ฐ€๋ฉด ๋น„๊ต์  ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค!

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

const len = Number(input[0]);
const queue=[];
const answer=[];

for(let i=1;i<=len;i++){
    let cmd=input[i].split(" ");
    switch(cmd[0]){
        case "push" : queue.push(cmd[1]);
        break;
        case "pop" : answer.push(queue.shift() || -1); //stack๊ณผ ๋ฐ˜๋Œ€๋กœ ๊ฐ€์žฅ ์•ž์—๊บผ๋ฅผ ๋นผ์ž
        break;
        case "size" : answer.push(queue.length);
        break;
        case 'empty' : answer.push(queue.length!==0? 0:1);
        break;
        case 'front': answer.push(queue[0]||-1);
        break;
        case 'back' : answer.push(queue[queue.length-1]||-1);
        break;
    }
}

console.log(answer.join("\n"));
Comments