본문 바로가기
백준

[백준] 10845번 파이썬 (큐)

by 헤이즐넛 좋아하는 개발자 2024. 3. 21.

1. 문제와 예제

문제
예제


2. 전체 코드

from collections import deque
import sys
input = sys.stdin.readline

N = int(input().rstrip()) # 명령의 수
deq = deque() # 큐

for _ in range(N):
    com = input().rstrip() # 명령
    l = len(deq) # 큐에 들어있는 정수의 개수
    # push X
    if com.split()[0] == 'push':
        deq.append(com.split()[1])
    # pop
    elif com == 'pop':
        if l == 0:
            print("-1")
        else:
            print(deq.popleft())
    # size
    elif com == 'size':
        print(l)
    # empty
    elif com == 'empty':
        if l == 0:
            print(1)
        else:
            print(0)
    # front
    elif com == 'front':
        if l == 0:
            print("-1")
        else:
            print(deq[0])
    # back
    else: # com == 'back'
        if l == 0:
            print("-1")
        else:
            print(deq[-1])

3. 풀이

10828번(스택) 문제가 스택의 가장 기초적인 문제였다면 이 문제가 큐의 가장 기초적인 문제다.