.Zzumbong

[leetCode/JS] 232. Implement Queue using Stacks ๋ณธ๋ฌธ

coding test/leetCode

[leetCode/JS] 232. Implement Queue using Stacks

์ญ˜๋ด‰ 2022. 12. 16. 11:23

๋‚œ์ด๋„ [ ๐Ÿ˜Š ] Easy

๋ฌธ์ œ ์„ค๋ช…

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).

Implement the MyQueue class:

  • void push(int x) Pushes element x to the back of the queue.
  • int pop() Removes the element from the front of the queue and returns it.
  • int peek() Returns the element at the front of the queue.
  • boolean empty() Returns true if the queue is empty, false otherwise.

Notes:

  • You must use only standard operations of a stack, which means only push to top, peek/pop from top, size, and is empty operations are valid.
  • Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
์Šคํƒ 2๊ฐœ๋ฅผ ์ด์šฉํ•ด์„œ ์„ ์ž…์„ ์ถœ์„ ํ•˜๋Š” queue๋ฅผ ๋งŒ๋“ค๊ธฐ์ด๋‹ค.
์–ธ์–ด์— ๋”ฐ๋ผ์„œ ์Šคํƒ์ด๋ผ๋Š” ํ‘œ์ค€ ์˜คํผ๋ ˆ์ด์…˜์ด ์žˆ์ง€๋งŒ.. js์—” ์—†๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ

Example 1:

Input
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]

Explanation
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

 

Constraints

  • 1 <= x <= 9
  • At most 100 calls will be made to push, pop, peek, and empty.
  • All the calls to pop and peek are valid.

 


 

๋‚ด ์†”๋ฃจ์…˜

  • ๋ฌธ์ œ๋ฅผ ๋ฌด์‹œํ•˜๊ณ  js์—์„œ ๊ทธ๋ƒฅ queue๋ฅผ ๋งŒ๋“ค๋ฉด ์ด๋ ‡๊ฒŒ ๋œ๋‹ค.
  • ์ด๋Ÿฐ๊ฑธ ์›ํ•œ๊ฑด ์•„๋‹ˆ๊ฒ ์ง€..
class MyQueue {
  constructor() {
    this.queue = [];
  }
  push(n) {
    if(!n) return;
    this.queue.push(n);
  }
  pop() {
    return this.queue.shift();
  }
  peek() {
    return this.queue[0];
  }
  empty() {
    return this.queue.length === 0;
  }
}

 

  • ์ง„์งœ๋กœ stack 2๊ฐœ๋ฅผ ์ด์šฉํ•œ ์„ ์ž…์„ ์ถœ ๋ฐฉ์‹.
  • push๋กœ ์ˆœ์„œ๋ฅผ ์ž˜ ๋งŒ๋“ค๋ฉด ๋‚˜๋จธ์ง„ ์‰ฝ๋‹ค.
class MyQueue {
  constructor() {
    this.stack = [];
    this.temp = [];
  }
  push(n) {
    while (this.stack.length > 0) { // ๋ฉ”์ธ ์Šคํƒ ๋ฐ์ดํ„ฐ๋ฅผ temp ์Šคํƒ์œผ๋กœ ์˜ฎ๊น€.
      this.temp.push(this.stack.pop());
    }
    this.temp.push(n); // temp์˜ ๋งˆ์ง€๋ง‰์— n์„ ๋„ฃ์Œ.
    while (this.temp.length > 0) { // temp๋ฅผ ์—ญ์ˆœ์œผ๋กœ stack์— ๋„ฃ์Œ.
      this.stack.push(this.temp.pop());
    }
  }
  pop() {
    return this.stack.pop();
  }
  peek() {
    return this.stack[this.stack.length-1];
  }
  empty() {
    return this.stack.length === 0;
  }
}
const st = new MyQueue();
st.push(1);
st.push(2);
st.push(3);

// stack : [3, 2, 1], temp: []

 

๊ฐ์ƒํ‰

  • ๋‹ค๋ฅธ ์–ธ์–ด์—์„  stack ์ด๋ผ๋Š” ๋ฐ์ดํ„ฐํ˜•์ด ์กด์žฌํ•˜๋‚˜๋ณด๋‹ค.
  • ๊ทผ๋ฐ JS์—์„  ์ด๋Ÿด ์ด์œ ๊ฐ€ 1๋„ ์—†์–ด์„œ ์ •~~๋ง ํ•˜๊ณ  ์‹ถ์ง€ ์•Š์•˜๋˜ ๋ฌธ์ œ.

'coding test > leetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[leetCode/JS] 739. Daily Temperatures  (0) 2022.12.18
[leetCode/JS] 150. Evaluate Reverse Polish Notation  (0) 2022.12.17
[leetCode/JS] 1143. Longest Common Subsequence  (0) 2022.12.15
[leetCode/JS] 152. Maximum Product Subarray  (0) 2022.12.14
[leetCode/JS] 198. House Robber  (0) 2022.12.14
Comments