🦖 Programming/Python
[Python] 백준 알고리즘 10845번 : 큐
박낑깡이
2022. 9. 27. 11:35
https://www.acmicpc.net/problem/10845
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
👩💻문제 이해
큐 자료구조를 사용하면 된다.
👩💻Python Deque를 사용한 코드 : 성공🌈
import sys
from collections import deque
N = int(sys.stdin.readline())
queue = deque()
for i in range(N):
command = sys.stdin.readline().split()
if command[0] == "push":
queue.append(command[1])
elif command[0] == "pop":
if not queue:
print(-1)
else:
print(queue.popleft())
elif command[0] == "size":
print(len(queue))
elif(command[0] == "empty"):
if not queue:
print(1)
else:
print(0)
elif(command[0] == "front"):
if not queue:
print(-1)
else:
print(queue[0])
✨Point✨
Deque 매서드
queue.append(item) | 오른쪽 끝에 삽입 |
queue.appendleft(item) | 왼쪽 끝에 삽입 |
queue.pop() | 가장 오른쪽의 요소 반환 및 삭제 |
queue.popleft() | 가장 왼쪽의 요소 반환 및 삭제 |
queue.extend(array) | 주어진 array 배열을 순회하면서 queue의 오른쪽에 추가 |
queue.extendleft(array) | 주어진 array 배열을 순회하면서 queue의 왼쪽에 추가 |
queue.remove(item) | 해당 item을 queue에서 찾아 삭제 |
queue.rotate(숫자) | 해당 숫자만큼 회전 (양수 : 시계방향, 음수 : 반시계 방향) |