[백준] #1021 회전하는 큐 python
2023. 4. 10. 18:59
https://www.acmicpc.net/problem/1021
1021번: 회전하는 큐
첫째 줄에 큐의 크기 N과 뽑아내려고 하는 수의 개수 M이 주어진다. N은 50보다 작거나 같은 자연수이고, M은 N보다 작거나 같은 자연수이다. 둘째 줄에는 지민이가 뽑아내려고 하는 수의 위치가
www.acmicpc.net
📕 설명 📕
python의 deque library를 사용하여 풀이하였다.
popleft, rotate의 function을 사용했다.
🧑🏻💻 나의 풀이 🧑🏻💻
from collections import deque
N, M = map(int,input().split())
data = deque([i for i in range(1,N+1)])
N_list = list(map(int,input().split()))
count = 0
for num in N_list:
while 1:
if data[0] == num:
data.popleft()
break
else:
if data.index(num) <= len(data)//2:
data.rotate(-1)
count += 1
else:
data.rotate(1)
count += 1
print(count)
'Programming > Algorithm' 카테고리의 다른 글
[백준] #5426 비밀 편지 python (0) | 2023.04.12 |
---|---|
[백준] #1057 토너먼트 python (0) | 2023.04.11 |
[백준] #1439 뒤집기 python (0) | 2023.04.07 |
[백준] #1476 날짜 계산 python (0) | 2023.04.06 |
[백준] #1531 투명 python (0) | 2023.04.05 |