Queue_카드2(2164)
문제
코드 - 덱 풀이
from collections import deque
n = int(input())
arr = deque(list(range(1,n+1)))
tmp = 0
while len(arr)>1:
arr.popleft()
tmp = arr.popleft()
arr.append(tmp)
print(arr.popleft())
코드 - 원형큐 풀이
arr = list(range(int(input())+1))
arrSize = len(arr)
front = 0
rear = arrSize-1
tmp = 0
def enqueue(x):
global rear
if (rear+1)%arrSize != front:
rear = (rear+1)%arrSize
arr[rear]=x
def dequeue():
global front
if front == rear:
print(tmp)
sys.exit()
front = (front+1)%arrSize
save = arr[front]
arr[front] = ""
return save
while rear!=front:
tmp = dequeue()
tmp = dequeue()
if(rear==front):
break
enqueue(tmp)
print(tmp)