코딩하는 포메라니안 2021. 8. 25. 00:03

1. 문제

 

 

 

2. 풀이 과정

방법 1) 큐에서 하나 꺼낼 때마다 location을 1 감소시켜서 현재 위치를 업데이트한다.

from collections import deque
def solution(priorities, location):
    Q = deque()
    for i in priorities:
        Q.append(i)
            
    count = 0
    while Q:
        a = Q.popleft()
        if Q and a < max(Q):
            Q.append(a)
        else:
            count += 1
            if location == 0:
                return count
        
        location -= 1
        if location < 0 : location = len(Q)-1
    return count

 

방법 2) 큐에 처음 인덱스 정보와 데이터를 함께 저장하고, any를 이용하여 큐에 우선순위가 자신보다 높은 값이 있는지 검사한다.

from collections import deque
def solution(priorities, location):
    answer = 0
    Q = deque()
    for i, j in enumerate(priorities):
        Q.append((i, j))
        
    while Q:
        a = Q.popleft()
        if any(a[1] < b[1] for b in Q):
            Q.append(a)
        else:
            answer += 1
            if a[0] == location:
                break
    return answer