[알고리즘] 큐, 덱 - 백준 18258번 큐2
정답 코드
import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
arr = deque()
for _ in range(N):
    command = input().split()
    if command[0] == 'push':
        arr.append(int(command[1]))
    elif command[0] == 'pop':
        if arr:
            print(arr.popleft())
        else:
            print(-1)
    elif command[0] == 'size':
        print(len(arr))
    elif command[0] == 'empty':
        if not arr:
            print(1)
        else:
            print(0)
    elif command[0] == 'front':
        if arr:
            print(arr[0])
        else:
            print(-1)
    elif command[0] == 'back':
        if arr:
            print(arr[-1])
        else:
            print(-1)큐 (Queue)
선입선출(FIFO)의 자료구조

( 출처: 큐(자료구조) 나무위키 )
먼저 들어오는 데이터가 먼저 나가게 되는 자료구조이다.
파이썬에서 Stack 은 일반 배열을 사용하지만 Queue 은 deque 를 사용하면 시간 효율성을 높일 수 있다.
from collections import deque
arr = deque()		// Queue 선언
arr.append(1)		// [1]
arr.append(2)		// [1, 2]
arr.appendleft(0)	// [0, 1, 2]
arr.pop()		// [0, 1]
arr.popleft()		// [1]Author And Source
이 문제에 관하여([알고리즘] 큐, 덱 - 백준 18258번 큐2), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@minidoo/알고리즘-큐-덱-백준-18258번-큐2저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)