링크 : https://www.acmicpc.net/problem/10866
10866번: 덱
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
- 문제
- 소요시간: 8분 8초
- 설계하기(접근방법)
1. n을 입력받는다.
2. n번만큼 명령을 입력받는다
3. 각자의 메서드를 구현한다.
- 코드(출력)
from collections import deque
import sys
que = deque([])
n = int(input())
for i in range(n):
order = sys.stdin.readline().split()
if order[0] == 'push_front':
que.appendleft(order[1])
if order[0] == 'push_back':
que.append(order[1])
if order[0] == 'pop_front':
if que:
print(que.popleft())
else:
print(-1)
if order[0] == 'pop_back':
if que:
print(que.pop())
else:
print(-1)
if order[0] == 'size':
print(len(que))
if order[0] == 'empty':
if que:
print(0)
else:
print(1)
if order[0] == 'front':
if que:
print(que[0])
else:
print(-1)
if order[0] == 'back':
if que:
print(que[-1])
else:
print(-1)
- 얻어갈 부분
1. deque의 기능과 자료구조를 알게 되었다
2. 시간초과가 되는 경우, string을 여러개 한번에 입력받는 경우 sys.stdin.readline을 사용하자
'알고리즘(백준) > 자료구조' 카테고리의 다른 글
[알고리즘/자료구조] 1966 번 : 프린터 큐 - python (0) | 2023.03.07 |
---|---|
[알고리즘/1935] 번 : 후위 표기식2 - python (0) | 2023.03.06 |
[알고리즘/자료구조] 2164번 : 카드2 - python (0) | 2023.03.05 |
[알고리즘/자료구조] 1158번 : 요세푸스 문제 - python (0) | 2023.03.04 |
[알고리즘/자료구조] 18258 번 : 큐 2 - python (0) | 2023.03.04 |