[백준] 18258번 큐 2

거북이·2023년 1월 11일
0

백준[실버4]

목록 보기
20/91
post-thumbnail

💡문제접근

  • from collections import deque를 사용해서 큐를 구현해야 시간초과가 발생하지 않는다.

💡코드(메모리 : 171244KB, 1396ms, PyPy3로 제출)

from collections import deque
import sys

li = deque([])
N = int(input())
for _ in range(N):
    command = list(map(str, sys.stdin.readline().strip().split()))
    if len(command) == 2:
        if command[0] == "push":
            li.append(int(command[1]))
    else:
        if command[0] == "pop":
            if len(li) == 0:
                print(-1)
            else:
                temp = li.popleft()
                print(temp)
        elif command[0] == "size":
            print(len(li))
        elif command[0] == "empty":
            if len(li) == 0:
                print(1)
            else:
                print(0)
        elif command[0] == "front":
            if len(li) == 0:
                print(-1)
            else:
                print(li[0])
        elif command[0] == "back":
            if len(li) == 0:
                print(-1)
            else:
                print(li[-1])

💡소요시간 : 2m

0개의 댓글