https://www.acmicpc.net/problem/10845
ํ์ด
# ๋ฐฑ์ค 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๋ฅผ ์ด์ฉํ์ฌ ๋ค์ ํ์๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] [Python] 1904๋ฒ_01ํ์ผ_๋์ ํ๋ก๊ทธ๋๋ฐ (0) | 2022.09.16 |
---|---|
[๋ฐฑ์ค] [Python] 15649๋ฒ_N๊ณผM(1)_ ๋ฐฑํธ๋ํน (0) | 2022.09.16 |
[๋ฐฑ์ค] [Python] 1920๋ฒ_์ ์ฐพ๊ธฐ (0) | 2022.03.19 |
[๋ฐฑ์ค] [Python] 7568๋ฒ_๋ฉ์น (0) | 2022.03.18 |
[๋ฐฑ์ค] [Python] 13305๋ฒ_์ฃผ์ ์ (0) | 2022.03.16 |