티스토리 뷰
https://www.acmicpc.net/problem/10866
10866번: 덱
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
👩💻deque 모듈을 사용한 코드 : 성공🌈
import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
queue = deque([])
for i in range(N):
command = input().split()
if command[0] == "push_front":
queue.appendleft(int(command[1]))
elif command[0] == "push_back":
queue.append(int(command[1]))
elif command[0] == "pop_front":
if queue:
print (queue.popleft())
else:
print (-1)
elif command[0] == "pop_back":
if queue:
print (queue.pop())
else:
print (-1)
elif command[0] == "size":
print (len(queue))
elif command[0] == "empty":
if queue:
print (0)
else:
print (1)
elif command[0] == "front":
if queue:
print (queue[0])
else:
print (-1)
elif command[0] == "back":
if queue:
print (queue[-1])
else:
print (-1)
'🦖 Programming > Python' 카테고리의 다른 글
[Python] 백준 알고리즘 1927번 : 최소 힙 (0) | 2022.10.05 |
---|---|
[Python] 백준 알고리즘 1966번 : 프린터 큐 (0) | 2022.10.04 |
[Python] 백준 알고리즘 10773번 : 제로 (1) | 2022.10.03 |
[Python] 백준 알고리즘 10845번 : 큐 (0) | 2022.09.27 |
[Python] 백준 알고리즘 4889번 : 안정적인 문자열 (0) | 2022.09.23 |
댓글