선릉역 1번 출구

baekjoon - 10845 본문

Algorithm/Algorithm 문제풀이

baekjoon - 10845

choideu 2021. 8. 30. 03:41
from collections import deque
import sys

deq = deque()

for _ in range(int(sys.stdin.readline().strip())):
    cmd = list(sys.stdin.readline().rstrip().split())
    if cmd[0] == "push":
        deq.append(cmd[1])
    elif cmd[0] == "pop":
        if deq:
            print(deq.popleft())
        else:
            print(-1)
    elif cmd[0] == "size":
        print(len(deq))
    elif cmd[0] == "empty":
        if deq:
            print(0)
        else:
            print(1)
    elif cmd[0] == "front":
        if deq:
            print(deq[0])
        else:
            print(-1)
    elif cmd[0] == "back":
        if deq:
            print(deq[-1])
        else:
            print(-1)

나는 deque를 사용해서 queue를 구현해주었다.

 

'Algorithm > Algorithm 문제풀이' 카테고리의 다른 글

baekjoon - 17413  (0) 2021.08.30
baekjoon - 10866  (0) 2021.08.30
baekjoon - 1406  (0) 2021.08.30
baekjoon - 1874  (0) 2021.08.28
baekjoon - 9012  (0) 2021.08.28
Comments