let's get IT with DAVINA ๐Ÿ’ป

[๋ฐฑ์ค€ #10866] ๋ฑ ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ #10866] ๋ฑ

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

โ“ ๋ฌธ์ œ

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

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

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

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

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

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

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

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


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

์ด๋ฒˆ ๋ฌธ์ œ๋„ ์Šคํƒ๊ณผ ํ๋ฅผ ์•Œ๋ฉด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ–ˆ๋‹ค. 

์Šคํƒ์€ ๋จผ์ € ๋“ค์–ด๊ฐ„๊ฒŒ ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋‚˜์™€, ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋“ค์–ด๊ฐ„๊ฒŒ ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์™€

ํ๋Š” ๋จผ์ € ๋“ค์–ด๊ฐ„๊ฒŒ ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์™€, ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋“ค์–ด๊ฐ„๊ฑด ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋‚˜์™€

๋ฑ์€ ์ฒ˜์Œ๊ณผ ๋ ์–‘์ชฝ์œผ๋กœ ๋‹ค ์ถ”๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•ด์š”, ์ ‘๊ทผ์ด ๋‹ค O(1)๋กœ ๊ฐ€๋Šฅ!

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

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

for (let i = 1; i <= len; i++) {
    let cmd = input[i].split(" ");
    switch (cmd[0]) {
        case "push_back":
            deque.push(cmd[1]);
            break;
        case "push_front":
            deque.unshift(cmd[1]);
            break;
        case "pop_front":
            answer.push(deque.shift() || -1);
            break;
        case "pop_back":
            answer.push(deque.pop() || -1);
            break;
        case "size":
            answer.push(deque.length);
            break;
        case "empty":
            answer.push(deque.length !== 0 ? 0 : 1);
            break;
        case "front":
            answer.push(deque[0] || -1);
            break;
        case "back":
            answer.push(deque[deque.length - 1] || -1);
            break;
    }
}

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