-
18258번: 큐 2
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
코드
import sys from collections import deque n = int(sys.stdin.readline()) q = deque([]) for i in range(n): s = sys.stdin.readline().split() if s[0] == 'push': q.append(s[1]) elif s[0] == 'pop': if not q: print(-1) else: print(q.popleft()) elif s[0] == 'size': print(len(q)) elif s[0] == 'empty': if not q: print(1) else: print(0) elif s[0] == 'front': if not q: print(-1) else: print(q[0]) elif s[0] == 'back': if not q: print(-1) else: print(q[-1])
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 11866번 요세푸스 문제 0 (파이썬) (0) 2021.12.28 [백준] 2164번 카드2 (파이썬) (0) 2021.12.28 [백준] 17298번 오큰수 (파이썬) (0) 2021.12.28 [백준] 1874번 스택 수열 (파이썬) (0) 2021.12.28 [백준] 4949번 균형잡힌 세상 (파이썬) (0) 2021.12.28