파이썬 알고리즘 : 프로세스

프로그래머스 Lv. 2

2023년 10월 11일 알고리즘 문제풀이

문제 1 프로세스

문제 링크

1차 시도

나의 생각

프로세스가 담긴 배열에서 첫번째가 우리가 알고싶은 프로세스 인지 아닌지, 또 가장 높은 우선순위를 지녀 실행될 차례인지, 아닌지로 구분하였다. location은 현재 우리가 알고싶은 프로세스의 index이고, answer는 몇번째로 실행되었는지를 의미한다.

  알고 싶은 프로세스 관심 없는 프로세스
최우선순위 answer 출력 popleft() 한다. 실행되었기 떄문에 answer 1 증가
차우선순위 popleft()하여 맨 뒤로 보낸다. location도 맨 뒤로 설정한다. popleft()후 맨 뒤로 보낸다. locaion 1 감소

결과

정답

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from collections import deque

def solution(priorities, location):
    answer = 0
    arr = deque()
    for x in priorities:
        arr.append(x)
    while arr:
        if location:
            if arr[0] == max(arr):
                arr.popleft()
                answer += 1
            else:
                tmp = arr.popleft()
                arr.append(tmp)
            location -= 1
        else:
            if arr[0] == max(arr):
                answer += 1
                return answer
            else:
                tmp = arr.popleft()
                arr.append(tmp)
                location += len(arr)-1