백준 10845

jeonghens·2023년 7월 24일

알고리즘: BOJ

목록 보기
6/125

문제

https://www.acmicpc.net/problem/10845

코드

import sys

queue = []

for i in range(int(sys.stdin.readline())):
    cmd = list(sys.stdin.readline().split())

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

기타

Queue의 기본 연산을 리스트로 구현하는 문제라 크게 어렵지 않았다.

profile
알고리즘이나 SQL 문제 풀이를 올리고 있습니다. 피드백 환영합니다!

0개의 댓글