https://school.programmers.co.kr/learn/courses/30/lessons/42587
"""
입출력 예시)
priorities = [2, 1, 3, 2], location = 2 -> 1
priorities = [1, 1, 9, 1, 1, 1], location = 0 -> 5
"""
from collections import deque
def solution(priorities, location):
answer = 0
d = deque([(v,i) for i,v in enumerate(priorities)]) # v: value, i: index
while len(d):
item = d.popleft() # popleft(): 맨 첫 번째 요소 제거 후 반환
if d and max(d)[0] > item[0]:
d.append(item)
else:
answer += 1
if item[1] == location:
break
return answer
-> deque 이용
- deque
- 큐의 앞, 뒤에서 삽입, 삭제가 가능한 큐 (double-ended queue)
https://velog.io/@nayoon-kim/%ED%8C%8C%EC%9D%B4%EC%8D%AC-deque
- d = deque([(v,i) for i,v in enumerate(priorities)])
# priorities = [1, 1, 9, 1, 1, 1], location = 0
d = deque([(v,i) for i,v in enumerate(priorities)])
"""
d = deque([(1, 0), (1, 1), (9, 2), (1, 3), (1, 4), (1, 5)])
"""
# priorities = [1, 1, 9, 1, 1, 1], location = 0
d = deque([(v,i) for i,v in enumerate(priorities)])
while len(d):
item = d.popleft() # popleft(): 맨 첫 번째 요소 제거 후 반환
print('item:', item)
"""
item: (1, 0)
item: (1, 1)
item: (9, 2)
item: (1, 3)
item: (1, 4)
item: (1, 5)
"""