[프로그래머스] 프린터 문제 풀이(스택/큐 Lv.2) - 파이썬 python

2021. 12. 13. 05:00네카라쿠배 취준반 - 프로그래머스 문제 풀이

0. 자세한 설명은 YouTube 영상으로

1-1. Queue + Enumerate 를 활용한 Solution

def solution(priorities, location):
    # 1. Queue를 만든다.
    printer = [(i,p) for i,p in enumerate(priorities)]
    turn = 0
    while printer:
        job = printer.pop(0)
        # 2. 나보다 중요한 job이 있으면 뒤에 넣는다.
        if any(job[1] < other_job[1] for other_job in printer):
            printer.append(job)
        else:
            turn+=1
            # 3. 내가 제일 중요하다면 수행하고 location과 비교한다.
            if job[0] == location:
                break;
    
    return turn


print(solution([2, 1, 3, 2], 0))

 

 

 

 

 

 

 

  • YouTube 영상에 자세한 내용을 정리했으니 참고하세요.
반응형