목차

ps
링크acmicpc.net/…
출처BOJ
문제 번호10866
문제명
레벨실버 4
분류

시간복잡도O(n)
인풋사이즈n<=10000
사용한 언어Python
제출기록32084KB / 120ms
최고기록64ms
해결날짜2021/08/21

풀이

코드

"""Solution code for "BOJ 10866. 덱".

- Problem link: https://www.acmicpc.net/problem/10866
- Solution link: http://www.teferi.net/ps/problems/boj/10866

Tags: [deque]
"""

import collections
import sys


def main():
    N = int(sys.stdin.readline())
    deq = collections.deque()
    for _ in range(N):
        oper = sys.stdin.readline().split()
        op = oper[0]
        if op == 'push_front':
            deq.appendleft(oper[1])
        elif op == 'push_back':
            deq.append(oper[1])
        elif op == 'pop_front':
            print(deq.popleft() if deq else '-1')
        elif op == 'pop_back':
            print(deq.pop() if deq else '-1')
        elif op == 'size':
            print(len(deq))
        elif op == 'empty':
            print('0' if deq else '1')
        elif op == 'front':
            print(deq[0] if deq else '-1')
        elif op == 'back':
            print(deq[-1] if deq else '-1')


if __name__ == '__main__':
    main()