1. 문제와 예제
2. 전체 코드
from collections import deque
import sys
input = sys.stdin.readline
N = int(input().rstrip()) # 명령의 수
deq = deque() # 큐
for _ in range(N):
com = input().rstrip() # 명령
l = len(deq) # 큐에 들어있는 정수의 개수
# push X
if com.split()[0] == 'push':
deq.append(com.split()[1])
# pop
elif com == 'pop':
if l == 0:
print("-1")
else:
print(deq.popleft())
# size
elif com == 'size':
print(l)
# empty
elif com == 'empty':
if l == 0:
print(1)
else:
print(0)
# front
elif com == 'front':
if l == 0:
print("-1")
else:
print(deq[0])
# back
else: # com == 'back'
if l == 0:
print("-1")
else:
print(deq[-1])
3. 풀이
10828번(스택) 문제가 스택의 가장 기초적인 문제였다면 이 문제가 큐의 가장 기초적인 문제다.
'백준' 카테고리의 다른 글
[백준] 2108번 파이썬 (통계학) (0) | 2024.03.21 |
---|---|
[백준] 10866번 파이썬 (덱) (0) | 2024.03.21 |
[백준] 10828번 파이썬 (스택) (0) | 2024.03.21 |
[백준] 10816번 파이썬 (숫자 카드 2) (0) | 2024.03.21 |
[백준] 10773번 파이썬 (제로) (0) | 2024.03.20 |