๐Ÿ—๏ธ Algorithm/๐ŸŸฉ ๋ฐฑ์ค€

[๋ฐฑ์ค€] [Python] 10845๋ฒˆ_ํ

Dbswnstjd 2022. 3. 24. 17:08

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

 

10845๋ฒˆ: ํ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net

ํ’€์ด

# ๋ฐฑ์ค€ 10845๋ฒˆ ๋ฌธ์ œ - ํ
import collections 
import sys

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

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

์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ช‡๋ฒˆ ๋‚˜์™€์„œ ๋‹นํ™ฉ ํ•˜์˜€์ง€๋งŒ deque๋ฅผ ์ด์šฉํ•˜์—ฌ ๋‹ค์‹œ ํ’€์—ˆ๋‹ค.