토니의 연습장
큐 예제 본문
https://www.acmicpc.net/problem/18258
Dequeue 활용
import sys
from collections import deque
N = int(input())
# N = int(sys.stdin.readline().strip())
d = deque([])
for i in range(N):
# input() # 입력 여러 개가 들어올 때는 readline이 더 빨라 권고됨
command = sys.stdin.readline()
command = command.split()
match command[0]:
case "push":
d.append(command[1])
case "pop":
if len(d) == 0:
print("-1")
else:
print(d.popleft())
case "size":
print(len(d))
case "empty":
if len(d) == 0:
print("1")
else:print("0")
case "front":
if len(d) == 0:
print("-1")
else:print(d[0])
case "back":
if len(d) == 0:
print("-1")
else:print(d[-1])
case _:
print("Unknown command")
https://www.acmicpc.net/problem/2164
N = int(input())
from collections import deque
d = deque([i+1 for i in range(N)])
drop = True
while len(d) > 1:
if drop:
d.popleft()
drop = False
else:
x = d.popleft()
d.append(x)
drop = True
print(d[0])
'Algorithm > Ch 1. 기초 자료구조' 카테고리의 다른 글
스택 예제 (0) | 2024.09.05 |
---|