Tesseractjh
한 걸음씩
Tesseractjh
전체 방문자
오늘
어제
  • 전체 (293)
    • IT (30)
      • JavaScript (7)
      • TypeScript (5)
      • React (5)
      • Next.js (3)
      • MongoDB (2)
      • Webpack (2)
      • HTML & CSS (1)
      • Git (0)
      • AWS (1)
      • 기타 (4)
    • 연습장 (259)
      • 백준(BOJ) 문제풀이 (185)
      • 프로그래머스 문제풀이 (61)
      • LeetCode 문제풀이 (2)
      • HackerRank 문제풀이 (7)
      • 낙서장 (3)
      • 기타 (1)
    • 프로젝트 (3)
      • 지뢰피하기 (1)
      • 키릴-라틴 문자 변환기 (1)
      • Flex & Grid (1)
    • 멋쟁이사자처럼 프론트엔드 스쿨 1기 (1)
      • 일기 & 회고록 (1)

인기 글

티스토리

hELLO · Designed By 정상우.
Tesseractjh

한 걸음씩

연습장/백준(BOJ) 문제풀이

[백준 18258] 큐 2 with Node.js

2021. 4. 25. 15:13

문제 링크

www.acmicpc.net/problem/18258

 

18258번: 큐 2

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

풀이

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

class Queue {
    constructor() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    createNode(value, prev, next) {
        return {
            value,
            prev,
            next
        };
    }

    push(value) {
        const curNode = this.createNode(value, this.tail, this.head);
        if (this.head) {
            this.tail.next = curNode;
            this.head.prev = curNode;
            this.tail = curNode;
        } else {
            this.head = curNode;
            this.tail = curNode;
            curNode.prev = curNode;
            curNode.next = curNode;
        }
        this.size++;
    }

    pop() {
        if (this.size > 2) {
            const value = this.head.value;
            const newHead = this.head.next;
            this.head = newHead;
            newHead.prev = this.tail;
            this.tail.next = this.head;
            this.size--;
            return value;
        } else if (this.size === 2) {
            const value = this.head.value;
            this.head = this.tail;
            this.tail.prev = this.tail;
            this.tail.next = this.tail;
            this.size--;
            return value;
        } else if (this.size === 1) {
            const value = this.head.value;
            this.head = null;
            this.tail = null;
            this.size--;
            return value;
        } else {
            return -1;
        }
    }

    empty() {
        return this.size ? 0 : 1;
    }

    front() {
        return this.head ? this.head.value : -1;
    }

    back() {
        return this.tail ? this.tail.value : -1;
    }
}

const myQueue = new Queue();
const output = [];

for (let i=1; i<input.length; i++) {
    const [command, value] = input[i].split(" ");
    switch(command) {
        case "push": myQueue.push(value); break;
        case "pop": output.push(myQueue.pop()); break;
        case "size": output.push(myQueue.size); break;
        case "empty": output.push(myQueue.empty()); break;
        case "front": output.push(myQueue.front()); break;
        case "back": output.push(myQueue.back()); break;
    }
}
console.log(output.join("\n"));

Queue 클래스를 구현하여 해결하였다.

저작자표시 비영리 (새창열림)

'연습장 > 백준(BOJ) 문제풀이' 카테고리의 다른 글

[백준 1120] 문자열 with Node.js  (0) 2021.04.26
[백준 9461] 파도반 수열 with Python  (0) 2021.04.26
[백준 11727] 2×n 타일링 2 with Python  (0) 2021.04.25
[백준 11656] 접미사 배열 with Node.js  (0) 2021.04.22
[백준 2193] 이친수 with Python  (0) 2021.04.22
    '연습장/백준(BOJ) 문제풀이' 카테고리의 다른 글
    • [백준 1120] 문자열 with Node.js
    • [백준 9461] 파도반 수열 with Python
    • [백준 11727] 2×n 타일링 2 with Python
    • [백준 11656] 접미사 배열 with Node.js
    Tesseractjh
    Tesseractjh
    바닐라 자바스크립트를 좋아하는 개발자입니다

    티스토리툴바