본문 바로가기

알고리즘/백준-파이썬

[백준] 10866번 (python 파이썬)

https://www.acmicpc.net/problem/10866

 

10866번: 덱

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

from collections import deque
import sys

order_count = int(input())
queue = deque()

for _ in range(order_count):
  order = sys.stdin.readline().strip()
  if order.startswith("push_f"):
    queue.appendleft(order.split()[1])
  elif order.startswith("push_b"):
    queue.append(order.split()[1])
  elif order.startswith("pop_f"):
    print(queue.popleft() if queue else -1)
  elif order.startswith("pop_b"):
    print(queue.pop() if queue else -1)
  elif order.startswith("s"):
    print(len(queue))
  elif order.startswith("e"):
    print(0 if queue else 1)
  elif order.startswith("f"):
    print(queue[0] if queue else -1)
  else:
    print(queue[-1] if queue else -1)

 

collections 모듈의 deque 객체(큐)를 사용할 수 있으면 쉬운 문제이다.