Algorithm/프로그래머스

스택/큐 - 프로세스 (프로그래머스)

jun_code 2025. 1. 18. 16:35

<문제>

https://school.programmers.co.kr/learn/courses/30/lessons/42587

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

<코드>

from collections import deque

def solution(priorities, location):
    prior_list = deque()
    answer = []
    l = len(priorities)
    prior = deque(sorted(priorities, reverse = True))
    for i in range(l):
        prior_list.append((priorities[i], i))
    
    M = prior.popleft()
    while len(prior_list) > 0:
        p = prior_list.popleft()
        if M != p[0]:
            prior_list.append(p)
        else:
            answer.append(p)
            if p[1] == location:
                return len(answer)
                break
            if len(prior) != 0:
                M = prior.popleft()

 

 

<풀이>

  • 프로세스 정보를 계속 뒤에 쌓아주어야하고 앞쪽 정보를 가져와야하므로 STACK이 아닌 QUEUE를 이용
  • WHILE문을 돌며 우선순위가 높은 프로세스가 오면 answer로 결과 반환