python-algorithm

백준 18258 큐 2

무적김두칠 2022. 11. 9. 20:02

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

 

18258번: 큐 2

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

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys
from collections import deque
 
n=int(input())
commands=deque()
for i in range(n):
    command = sys.stdin.readline().rstrip()
    if command =='front':
        if len(commands) ==0 :
            print(-1)
        else:
            print(commands[0])
    elif command =='back':
        if len(commands) ==0 :
            print(-1)
        else:
            print(commands[-1])
    elif command =='empty':
        if len(commands) ==0 :
            print(1)
        else:
            print(0)
    elif command == 'size':
        print(len(commands))
    elif command == 'pop':
        if len(commands) ==0 :
            print(-1)
        else:
            print(commands.popleft())
    else:
        push, num= command.split()
        commands.append(int(num))
cs

큐를 구현하는 과정이라고 보심 되겠어요
속도 이슈로 인해서 입력은 sys.stdin.readline()으로 받으셔요

This is about implementing Queue(Deque in python)
Within time limit  Use not input() but sys.stdin.readline()

반응형