아이공의 AI 공부 도전기

[Baekjoon] 18258번 : 큐 2 (Python, 큐)

 

     

 

 

 

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

 

18258번: 큐 2

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

www.acmicpc.net

코드 링크

https://github.com/stellaluminary/Baekjoon

 

GitHub - stellaluminary/Baekjoon

Contribute to stellaluminary/Baekjoon development by creating an account on GitHub.

github.com

 

Python

 

방법 1 - 메모리 139604KB / 시간 1844ms / 코드 길이 710B

 

collections.deque 내장 함수를 활용하여 큐에 조건에 맞게 풀이한다.

 

from collections import deque
import sys

input = sys.stdin.readline
n = int(input())
q = deque()

for i in range(n):
    t = input().split()
    #print(t, q)
    if t[0] == 'push':
        q.append(t[1])
    elif t[0] == 'pop':
        if len(q) == 0:
            print(-1)
        else:
            a = q.popleft()
            print(a)
    elif t[0] == 'front':
        if len(q) == 0:
            print(-1)
        else:
            print(q[0])
    elif t[0] == 'back':
        if len(q) == 0:
            print(-1)
        else:
            print(q[-1])
    elif t[0] == 'size':
        print(len(q))
    elif t[0] == 'empty':
        if len(q) == 0:
            print(1)
        else:
            print(0)

 

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading