[Python] 백준10845번 : 큐

hjeu·2025년 2월 3일

백준

목록 보기
26/48
post-thumbnail

💡문제

백준 10846번 문제 링크

🍀풀이

코드

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

n = int(input())
q = deque()

for _ in range(n):
    command = input().strip().split()
    
    if command[0] == 'push':
        q.append(command[1])
    elif command[0] == 'pop':
        if q:
            print(q.popleft())
        else:
            print(-1)
    elif command[0] == 'size':
        print(len(q))
    elif command[0] == 'empty':
        if not q:
            print(1)
        else:
            print(0)
    elif command[0] == 'front':
        if q:
            print(q[0])
        else:
            print(-1)
    elif command[0] == 'back':
        if q:
            print(q[-1])
        else:
            print(-1)

파이썬에서 큐를 나타내는 방법이 listdeque를 쓰는 두가지 방법이 있다.
deque가 연산 속도가 더 빠르다고 하니 이거를 써서 풀어봤다.

풀면서 안 사실

  1. 큐는 LIFO니까 pop할 때, popleft()를 써야함 (아무생각 없이 pop() 썼다가 틀렸다고 나와서 뭔가 했다..)
  2. deque에는 size()와 empty() 함수가 없다! 왜일까...?

profile
나는야 개발왕이 될거야! (๑ •̀ω•́)۶

0개의 댓글