[백준] 10866번 덱

거북이·2023년 1월 11일
0

백준[실버4]

목록 보기
13/91
post-thumbnail

💡문제접근

  • from collections import deque를 이용해서 덱 자료 구조를 구현하는 문제였다.

💡코드(메모리 : 34152KB, 시간 : 76ms)

from collections import deque
import sys

li = deque([])
N = int(input())
for _ in range(N):
    command = list(map(str, sys.stdin.readline().strip().split()))
    if len(command) == 2:
        if command[0] == "push_front":
            li.appendleft(int(command[1]))
        elif command[0] == "push_back":
            li.append(int(command[1]))
    else:
        if command[0] == "pop_front":
            if li == deque([]):
                print(-1)
            else:
                temp = li.popleft()
                print(temp)
        elif command[0] == "pop_back":
            if li == deque([]):
                print(-1)
            else:
                temp = li.pop()
                print(temp)
        elif command[0] == "size":
            print(len(li))
        elif command[0] == "empty":
            if li == deque([]):
                print(1)
            else:
                print(0)
        elif command[0] == "front":
            if li == deque([]):
                print(-1)
            else:
                print(li[0])
        elif command[0] == "back":
            if li == deque([]):
                print(-1)
            else:
                print(li[-1])

💡소요시간 : 4m

0개의 댓글