*문제 출처는 백준에 있습니다.
문제 제목: 덱 / 10866번 (실버 4단계)
문제 사이트: https://www.acmicpc.net/problem/10866
문제 설명

나의 풀이
from collections import deque
def solution():
dq = deque()
n = int(input())
result = []
for _ in range(n):
command = input().split()
if command[0] == 'push_front':
dq.appendleft(int(command[1]))
elif command[0] == 'push_back':
dq.append(int(command[1]))
elif command[0] == 'pop_front':
if dq:
result.append(dq.popleft())
else:
result.append(-1)
elif command[0] == 'pop_back':
if dq:
result.append(dq.pop())
else:
result.append(-1)
elif command[0] == 'size':
result.append(len(dq))
elif command[0] == 'empty':
result.append(0 if dq else 1)
elif command[0] == 'front':
result.append(dq[0] if dq else -1)
elif command[0] == 'back':
result.append(dq[-1] if dq else -1)
for i in result:
print(i)
if __name__ == "__main__":
solution()

※ 알아야 할 것
자료구조를 물어보는 문제입니다.
'Coding Test > 백준-Python' 카테고리의 다른 글
| 백준 / 쇠막대기 / 10799번 / Python (0) | 2025.06.12 |
|---|---|
| 백준 / 제곱수의 합 / 1699번 / Python (0) | 2025.06.10 |
| 백준 / 촌수계산 / 2644번 / Python (0) | 2025.06.06 |
| 백준 / 킥다운 / 1195번 / Python (0) | 2025.06.02 |
| 백준 / 스택 수열 / 1874번 / Python (0) | 2025.06.02 |