알고리즘

[백준, 파이썬, 18258번] 큐 2

hminor 2023. 9. 2. 11:37

 

풀이

  • deque를 사용하여
    • pop을 popleft()로 사용해서 풀이 
  • 파이썬에서는 switch가 없기에 조건문이 너무 길어서
    • 따로 함수로 빼내어 가독성을 그나마 좋게 하려고 했음

 

def check(_inVal):
    if _inVal[0] == 'push': q.append(_inVal[1])
    elif _inVal[0] == 'pop':
        if len(q): print(q.popleft())
        else: print(-1)
    elif _inVal[0] == 'size': print(len(q))
    elif _inVal[0] == 'empty':
        if len(q): print(0)
        else: print(1)
    elif _inVal[0] == 'front':
        if len(q): print(q[0])
        else: print(-1)
    elif _inVal[0] == 'back':
        if len(q): print(q[-1])
        else: print(-1)
    return

import sys
from collections import deque
input = sys.stdin.readline
q = deque([])
for _ in range(int(input())):
    _inVal = input().rstrip('\n').split()
    check(_inVal)