Algorithm/프로그래머스

[프로그래머스] 프로세스 - 파이썬

potato_pizza 2024. 6. 25. 17:01
728x90

프로세스

https://school.programmers.co.kr/learn/courses/30/lessons/42587?language=python3

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

문제

풀이

  • queue를 활용한 풀이
  • index와 우선순위 값(value)를 enumerate를 활용해 같이 queue에 집어넣기
  • 큐가 비어있지 않을 때 최고값 비교
from collections import deque

def solution(priorities, location):
    answer = 0
    queue = deque([(idx, value) for idx, value in enumerate(priorities)])

    while queue:
        item = queue.popleft()
        # 큐에서 최대 우선순위를 찾는 부분
        if queue:  # 큐가 비어있지 않을 때만 최대값 비교
            max_priority = max([priority for idx, priority in queue])
            if max_priority > item[1]:
                queue.append(item)
            else:
                answer += 1
                if item[0] == location:
                    break
        else:  # 큐가 비어있다면 현재 아이템이 최고 우선순위
            answer += 1
            if item[0] == location:
                break

    return answer
728x90
반응형