DevKim

[Python] 프로그래머스 LV.02 - 프린터 본문

알고리즘 PS

[Python] 프로그래머스 LV.02 - 프린터

on_doing 2021. 2. 11. 20:19
728x90

programmers.co.kr/learn/courses/30/lessons/42587

 

코딩테스트 연습 - 프린터

일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린

programmers.co.kr

[ 자료구조 ]

스택,큐

 

[문제 접근]

가장 앞에 있는 문서(J)를 큐에서 꺼낸 후, 중요도가 가장 높은 애 (max) 보다 작으면 다시 큐에 넣어줌

큐가 빌 때까지 반복

 

 

[코드]

from collections import deque
def solution(priorities, location):
    que=deque()
    cnt=0
    
    for i in range(len(priorities)):
        que.append((priorities[i],i))
        
    while que:
        pri,idx=que.popleft()
        
        if len(que)>0:
            m=max(que)

            if pri >= m[0]:
                cnt+=1
                if idx==location:
                    answer=cnt
                    break

            else:
                que.append((pri,idx)) #다시 넣어줌
        
        elif len(que)==0:
            answer=cnt+1
            
                
    return answer
728x90
Comments