백준 28279 파이썬 또 시간 초과

2024. 5. 7. 08:27코딩/백준 (단계별)

반응형

백준 28279 - 덱 2

문제

https://www.acmicpc.net/problem/28279

 

28279번: 덱 2

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

www.acmicpc.net

28279번

 

답안 코드 :

from collections import deque

import sys

# n = int(sys.stdin.readline())

input = sys.stdin.readline

n = int(input())

deq = deque()

for _ in range(n):
    command = input().split()

    if command[0] == "1":
        deq.appendleft(int(command[1]))
    elif command[0] == "2":
        deq.append(int(command[1]))
    elif command[0] == "3":
        if deq:
            print(deq.popleft())
        else:
            print(-1)
    elif command[0] == "4":
        if deq:
            print(deq.pop())
        else:
            print(-1)
    elif command[0] == "5":
        print(len(deq))
    elif command[0] == "6":
        print(1 if not deq else 0)
    elif command[0] == "7":
        if deq:
            print(deq[0])
        else:
            print(-1)
    elif command[0] == "8":
        if deq:
            print(deq[-1])
        else:
            print(-1)

 

백준 / 문제 / 단계별로 풀어보기 / 16단계 스택, 큐, 덱

생각 :

# 또 시간초과가 나서
# import sys

# n = int(sys.stdin.readline())

input = sys.stdin.readline

n = int(input())

# 이걸로 또 해결함!

 

반응형