입력 받을 때, sys.stdin.readline().strip()을 써줘야 속도가 빨라진다.
큐 사용을 위해 deque를 가져왔다.
큐가 비어있는지 확인하려면 if que: 로 확인해주면 된다.
비어있으면 False를, 뭔가 들어있으면 True를 반환한다.
import sys
from collections import deque
t = int(input())
que = deque()
for _ in range(t):
# print(que)
instruct = sys.stdin.readline().strip()
if instruct[:4] == 'push':
que.append(instruct[5:])
elif instruct[:3] == 'pop':
if que:
r = que.popleft()
print(r)
else:
print(-1)
elif instruct[:4] == 'size':
print(len(que))
elif instruct[:4] == 'empt':
if que:
print(0)
else:
print(1)
elif instruct[:4] == 'fron':
if que:
print(que[0])
else:
print(-1)
elif instruct[:4] == 'back':
if que:
print(que[-1])
else:
print(-1)
'백준' 카테고리의 다른 글
균형잡힌 세상 - 4949번 (0) | 2022.10.26 |
---|---|
덱 - 10866번 (0) | 2022.10.26 |
프린터 큐 - 1966번 (0) | 2022.10.24 |
통계학 - 2108번 (0) | 2022.10.24 |
블랙잭 - 2798번 (0) | 2022.10.22 |
댓글