파이썬 문제풀이

[백준 파이썬] 28279 덱 2

냄비짱 2023. 10. 13. 05:03
728x90

📌문제 출처

백준 단계별 문제풀이 : 스택, 큐, 덱 단계
https://www.acmicpc.net/problem/28279

 

28279번: 덱 2

첫째 줄에 명령의 수 N이 주어진다. (1 ≤ N ≤ 1,000,000) 둘째 줄부터 N개 줄에 명령이 하나씩 주어진다. 출력을 요구하는 명령은 하나 이상 주어진다.

www.acmicpc.net

❓ 문제

 

📗 풀이 코드

'''
deque를 쓰지 않고 list에서 pop, insert 사용시 시간초과
'''
from collections import deque
input = open(0).readline

if __name__ == '__main__':
    stack = deque()
    for _ in range(int(input())):
        ord = list(map(int,input().split()))
        if ord[0]==1: stack.appendleft(ord[1])
        elif ord[0]==2: stack.append(ord[1])
        elif ord[0]==3: print(stack.popleft() if stack else -1)
        elif ord[0]==4: print(stack.pop() if stack else -1)
        elif ord[0]==5: print(len(stack))
        elif ord[0]==6: print(0 if stack else 1)
        elif ord[0]==7: print(stack[0] if stack else -1)
        else : print(stack[-1] if stack else -1)