본문 바로가기

알고리즘_파이썬

백준 18258번: 큐 2

간단한 큐 구현 문제이다. 코드는 다음과 같다

import sys
from collections import deque

N = int(sys.stdin.readline().strip())
q = deque()

for _ in range(N):
    cmd = sys.stdin.readline().strip().split()

    if cmd[0] == 'push':
        q.append(cmd[1])  # Using append for queue behavior
    elif cmd[0] == 'pop':
        print(q.popleft() if q else -1)
    elif cmd[0] == 'size':
        print(len(q))
    elif cmd[0] == 'empty':
        print(0 if q else 1)
    elif cmd[0] == 'front':
        print(q[0] if q else -1)
    elif cmd[0] == 'back':
        print(q[-1] if q else -1)

'알고리즘_파이썬' 카테고리의 다른 글

백준 11866번: 요세푸스 문제  (0) 2025.03.11
백준 12789번: 도키도키 간식드리미  (0) 2025.03.11
백준 10828 스택  (1) 2024.02.07
백준 2884 알람 시계  (0) 2024.02.03
백준 1929 소수 구하기  (0) 2024.02.03