📖이 포스팅은 '파이썬 알고리즘 인터뷰 - 박상길님' 책을 보고 작성되었습니다.
😎문제 : https://leetcode.com/problems/implement-queue-using-stacks/submissions/
스택을 이용해 다음 연산을 지원하는 큐를 구현하라
[스택을 이용한 큐 구현]
class MyQueue:
def __init__(self):
self.input = []
self.output = []
def push(self, x: int) -> None:
self.input.append(x)
def pop(self) -> int:
self.peek()
return self.output.pop()
def peek(self) -> int:
#output이 없다면 모두 재입력
if not self.output:
while self.input:
self.output.append(self.input.pop())
return self.output[-1]
def empty(self) -> bool:
return self.input == [] and self.output == []
'자료구조와 알고리즘 > 🥑알고리즘' 카테고리의 다른 글
[알고리즘][데크 & 우선순위 큐] - 1. 원형 데크 디자인 (0) | 2022.03.04 |
---|---|
[알고리즘][스택 & 큐] - 6. 원형 큐 디자인 (0) | 2022.03.02 |
[알고리즘][스택 & 큐] - 4. 큐를 이용한 스택 구현 (0) | 2022.03.02 |
[알고리즘][스택 & 큐] - 3. 일일 온도 (0) | 2022.03.02 |
[알고리즘][스택 & 큐] - 2. 중복 문자 제거 (0) | 2022.03.01 |