[프로그래머스] 프린터 - python

코린이·2022년 6월 3일
0

프로그래머스

목록 보기
11/22

📢 "프린터" 문제

프로그래머스 문제 링크

🔎 풀이

사용 언어 : python

deque를 이용해서 해결

priorities[0] 값이 max가 아닐 경우 append로 맨 뒤로 보낸 후 popleft()로 삭제

location이 앞으로 하나씩 이동하므로 -1를 해준다.
location < 0인 경우는
priorites[0]이 요청한 문서였으나, max값이 아니라 맨뒤로 보낸 경우이다.
이 경우 locationlen(priorites)-1이다.
-1을 하는 이유는 location이 인덱스라고 생각하면된다.

🔎 코드

from collections import deque


def solution(priorities, location):
    priorities = deque(priorities)
    count = 0
    while 1:
        if priorities[0] == max(priorities):
            count += 1
            priorities.popleft()
            if location == 0:
                break
            else:
                location -= 1
        else:
            priorities.append(priorities[0])
            priorities.popleft()
            location -= 1

        if location < 0:
            location = len(priorities)-1
    return count
profile
초보 개발자

0개의 댓글