[Python] BOJ 10866번. 덱

10866번. 덱

문제 링크

풀이 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 10866번. 덱

# 시간초과 방지용 readline
from collections import deque
import sys
input = sys.stdin.readline

n = int(input())
dq = deque()
for i in range(n):
    s = input().strip()
    if s.split()[0] == 'push_front':
        dq.appendleft(s.split()[1])
    elif s.split()[0] == 'push_back':
        dq.append(s.split()[1])
    elif s == 'pop_front':
        if not dq:
            print(-1)
        else:
            print(dq.popleft())
    elif s == 'pop_back':
        if not dq:
            print(-1)
        else:
            print(dq.pop())
    elif s == 'size':
        print(len(dq))
    elif s == 'empty':
        if not dq:
            print(1)
        else:
            print(0)
    elif s == 'front':
        if not dq:
            print(-1)
        else:
            print(dq[0])
    elif s == 'back':
        if not dq:
            print(-1)
        else:
            print(dq[-1])

비고