CT

[Python] 백준 18258번 : 큐 2 파이썬

shur_ 2023. 9. 6. 01:42

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

 

18258번: 큐 2

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net


 


 

import sys
from collections import deque

N= int(sys.stdin.readline().rstrip())
queue = deque()

for i in range(N):
    
    command = sys.stdin.readline().rstrip().split()
    if command[0] == 'push':
        queue.append(command[1])
        
    elif command[0] == 'pop':
        if not queue :
            print(-1)
        else:
            print(queue.popleft())
            
    elif command[0] == 'size':
        print(len(queue))
        
    elif command[0] == 'empty':
        if not queue :
            print(1)
        else:
            print(0)
            
    elif command[0] == 'front':
        if not queue :
            print(-1)
        else:
            print(queue[0])
            
    elif command[0] == 'back':
        if not queue :
            print(-1)
        else:
            print(queue[-1])

 

 

 

https://docs.python.org/3/library/collections.html#collections.deque

 

collections — Container datatypes

Source code: Lib/collections/__init__.py This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.,,...

docs.python.org