😁 빅데이터 문제 풀기 & Study/- BAEKJOON 문제

[10845] 큐 / 파이썬 (deque 사용하기)

또방91 2021. 11. 29. 15:23
728x90

 

 

문제 링크: https://www.acmicpc.net/problem/10845

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,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
from collections import deque
from sys import stdin
 
n=int(stdin.readline())
pot=[]
pot=deque(pot)
    
for _ in range(n):
    text=stdin.readline().split()
    
    if text[0]=='push':
        pot.append(text[1])
    
    elif text[0]=='pop':
        if len(pot)!=0
            print(pot[0])
            pot.popleft()
        elseprint(-1)
        
    elif text[0]=='size'print(len(pot))
    
    elif text[0]=='empty':
        if len(pot)==0print(1)
        elseprint(0)
        
    elif text[0]=='front':
        if len(pot)!=0print(pot[0])
        elseprint(-1)
        
    elif text[0]=='back':
        if len(pot)!=0print(pot[-1])
        elseprint(-1)
cs

💯 풀이 과정

식 쓰는게 엄청 귀찮긴 하지만, 하나하나 적어내려가보면 쉬운 문제!

deque를 사용해서 popleft 빼주는 것이 이 문제의 핵심인 것 같당


😎오늘의 한줄평: 큐는 못 잊어

728x90