발행일

[99클럽] 99클럽 코테 스터디 4일차 + 스택과 큐

[99클럽] 99클럽 코테 스터디 4일차 + 스택과 큐

이 글은 네이버 블로그에 2025년 4월 3일에 올렸던 것을 그대로 옮겨온 것입니다.

  • 오늘의 학습 키워드

  • 공부한 내용 본인의 언어로 정리하기

  • 오늘의 회고

  • 어떤 문제가 있었고, 나는 어떤 시도를 했는지

  • 어떻게 해결했는지

  • 무엇을 새롭게 알았는지

  • 내일 학습할 것은 무엇인지

class MyQueue {
  constructor() {
    this.stackIn = [];
    this.stackOut = [];
  }

  push(x) {
    this.stackIn.push(x);
  }

  pop() {
    if (this.stackOut.length === 0) {
      while (this.stackIn.length > 0) {
        this.stackOut.push(this.stackIn.pop());
      }
    }
    return this.stackOut.pop();
  }

  peek() {
    if (this.stackOut.length === 0) {
      while (this.stackIn.length > 0) {
        this.stackOut.push(this.stackIn.pop());
      }
    }
    return this.stackOut[this.stackOut.length - 1];
  }

  empty() {
    return this.stackIn.length === 0 && this.stackOut.length === 0;
  }
}

필수 해시태그: #99클럽 #코딩테스트준비 #개발자취업 #항해99 #TIL

image