이 문제를 풀기 위해 내가 생각한 알고리즘은 다음과 같다.
sys.stdin.readline
으로 입력받는다.collections
모듈의 deque
를 이용하여 'push'와 ''pop' 명령을 구현한다.import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
que = deque()
for i in range(N):
instruction = list(input().split())
if len(instruction) == 2:
que.append(int(instruction[1]))
else:
if instruction[0] == 'pop':
if len(que) == 0:
print(-1)
else:
print(que.popleft())
elif instruction[0] == 'size':
print(len(que))
elif instruction[0] == 'empty':
print(int(len(que) == 0))
elif instruction[0] == 'front':
if len(que) == 0:
print(-1)
else:
print(que[0])
elif instruction[0] == 'back':
if len(que) == 0:
print(-1)
else:
print(que[-1])
이 문제를 풀기 위해 생각했던 알고리즘이다.
popleft
를 이용하여append
를 이용하여 그 다음 카드를 가장 뒤로 보낸다.import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
que = deque()
for i in range(N):
que.append(i+1)
while(len(que) > 1):
que.popleft()
move = que.popleft()
que.append(move)
print(que[0])
하루하루 꾸준하게 스택 쌓아보자.