본문 바로가기
Algorithm/프로그래머스

[프로그래머스] 파이썬 문제풀이 - 프로세스

by whdgus928 2023. 4. 25.

https://school.programmers.co.kr/learn/courses/30/lessons/42587?language=python3 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

첫 풀이

1. 큐를 만들어 우선순위가 높은 프로세스가 있으면 popleft하고 다시 append한다

2. idx=location을 설정해 알기 원하는 프로세스의 위치를 추적한다

from collections import deque
def solution(priorities, location):
    answer = 1
    q=deque(priorities)
    idx=location
    while len(q)>1:
        tmp=q.popleft()
        if tmp<max(q):
            q.append(tmp)
            if idx==0:
                idx=len(q)-1
            else:
                idx-=1
        else:
            if idx==0:
                return answer
            else:
                answer+=1
                idx-=1
                
    return answer

 

더 나은 답안

1. 튜플로 처음 순서를 기억한다

2. any로 조건을 체크한다

def solution(priorities, location):
    queue =  [(i,p) for i,p in enumerate(priorities)]
    answer = 0
    while True:
        cur = queue.pop(0)
        if any(cur[1] < q[1] for q in queue):
            queue.append(cur)
        else:
            answer += 1
            if cur[0] == location:
                return answer

 

배운 점

1. any를 사용하면 간편하게 조건을 체크할 수 있다

반응형

댓글