-
69. 큐 2Problem Solving 2023. 1. 14. 16:30728x90https://www.acmicpc.net/problem/18258
18258번: 큐 2
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
큐를 구현하는 문제
from collections import deque import sys queue1 = [] queue = deque(queue1) N = int(sys.stdin.readline()) for i in range(N): command = sys.stdin.readline()[:-1] if command == 'pop': if len(queue) == 0: print(-1) else: print(queue.popleft()) elif command == 'size': print(len(queue)) elif command == 'empty': if len(queue) == 0: print(1) else: print(0) elif command == 'front': if len(queue) == 0: print(-1) else: print(queue[0]) elif command == 'back': if len(queue) == 0: print(-1) else: print(queue[-1]) else: numb = command.split()[1] queue.append(numb)
deque 함수 이용해서 풂
sys.stdin.readline()[-1]을 사용하지 않으면 시간 초과 걸림
'Problem Solving' 카테고리의 다른 글
71. Implement Queue using Stacks (0) 2023.01.14 70. 카드2 (0) 2023.01.14 68. 크레인 인형뽑기 게임 (1) 2023.01.09 67. Remove Outermost Parenthesis (1) 2023.01.09 66. Next Greater Element I (0) 2023.01.09