Algorithm/Queue
[Baekjoon 18258 / python / 실버4] 큐 2
양선규
2024. 3. 26. 19:52
728x90
반응형
import sys
from collections import deque
n = int(sys.stdin.readline())
home = deque()
for _ in range(n):
control = sys.stdin.readline().split()
c = control[0]
if c == 'push':
home.append(int(control[1]))
elif c == 'pop':
if len(home) == 0:
print(-1)
else:
print(home.popleft())
elif c == 'size':
print(len(home))
elif c == 'empty':
if len(home) == 0:
print(1)
else:
print(0)
elif c == 'front':
if len(home) == 0:
print(-1)
else:
print(home[0])
elif c == 'back':
if len(home) == 0:
print(-1)
else:
print(home[-1])
큐를 구현하여 사용하는 문제이다. 스택 문제와 매우 흡사하다.
풀이는 간단하다. 그냥 문제에서 제시하는 명령을 큐로써 동작하게 하기만 하면 된다.
다만 이 문제를 풀기 위하여 큐 자료구조 공부가 따로 필요할 수 있기 때문에 난이도가 실버로 설정된 것으로 보인다.
728x90
반응형