[백준] 10845번 큐

거북이·2023년 1월 10일
0

백준[실버4]

목록 보기
10/91
post-thumbnail

💡문제접근

  • 명령의 길이에 따라 구분하고 각 명령을 수행하는 코드를 작성했다.

💡코드(메모리 : 30616KB, 시간 : 52ms)

import sys

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

💡소요시간 : 2m

0개의 댓글