[BOJ] 18258 큐 2

[BOJ] 18258 큐 2

문제 링크

자료구조 큐를 익히기 위한 문제 파이썬에서는 deque를 이용해서 풀이하는 것이 자원관리에 효율적이기에 dequeimport하여 진행

나의 풀이

import sys
from collections import deque

input = sys.stdin.readline

arr = deque()
result = []

n = int(input())

for _ in range(n):
  command = input().strip()
  # push
  if len(command.split()) > 1:
    arr.append(int(command.split()[1]))
  # others
  else:
    if command == "pop":
      if len(arr):
        result.append(arr.popleft())
      else:
        result.append(-1)
    elif command == "size":
      result.append(len(arr))
    elif command == "empty":
      if len(arr) == 0:
        result.append(1)
      else:
        result.append(0)
    elif command == "front":
      if len(arr):
        result.append(arr[0])
      else:
        result.append(-1)
    elif command == "back":
      if len(arr):
        result.append(arr[-1])
      else:
        result.append(-1)    

for v in result:
  print(v)

© 2021. All rights reserved.