백준 1021 파이썬 회전하는 큐

Screen Shot 2021-05-14 at 9.10.37 PM.png

회전하는 큐

 

왼쪽으로 돌릴 때는 index가 0인 원소를 뒤에 넣고 앞을 삭제,

오른쪽으로 돌릴 때는 index가 -1인 원소를 앞에 넣고 뒤를 삭제하면 된다

 

import sys
N, M = map(int, sys.stdin.readline().split())
K = list(map(int, sys.stdin.readline().split()))
A = [i for i in range(1, N + 1)]
cnt = 0
for i in range(M):
A_len = len(A)
A_index = A.index(K[i])
if A_index < A_len - A_index:
while True:
if A[0] == K[i] :
del A[0]
break
else :
A.append(A[0])
del A[0]
cnt += 1
else:
while True:
if A[0] == K[i] :
del A[0]
break
else:
A.insert(0, A[-1])
del A[-1]
cnt += 1
print(cnt)

Screen Shot 2021-05-14 at 9.23.52 PM.png

'공부하자! > 알고리즘' 카테고리의 다른 글

백준 5622 파이썬 다이얼  (0) 2021.05.20
백준 2606 파이썬 바이러스  (0) 2021.05.14
백준 17298 파이썬 오큰수  (0) 2021.05.12
백준 10250 ACM 호텔  (0) 2021.03.12
백준 1009 파이썬 분산처리  (0) 2021.03.11