728x90
풀이
def solution(priorities, location):
answer = 0
countPrior = [0 for i in range(9)]
for i in range(len(priorities)):
countPrior[priorities[i] - 1] += 1
index = 0
curPrior = 9
while(True):
if countPrior[curPrior - 1] == 0:
curPrior -= 1
else:
if priorities[index] == curPrior:
answer += 1
countPrior[curPrior - 1] -= 1
if index == location:
break
if countPrior[curPrior - 1] == 0:
curPrior -= 1
index = (index + 1) % len(priorities) #원형 큐 구현
return answer
내 풀이
1. 우선순위 별로 작업이 몇 개 있는지 countPrior 배열에 저장.
- 예를 들어 우선순위 1인 작업의 수는 countPrior[0]에 저장됨
2. 작업이 저장된 큐(priorities)를 원형 큐처럼 돌면서 location 에 있는 작업이 언제 리턴되는지 계산.
- index는 현재 큐에서 가리키는 인덱스의 위치, curPrior은 현재 실행하는 작업의 우선순위(9부터 내려감)
- location에 가기까지 실행된 작업은 answer+=1로 추가.
- 실행된 작업 수만큼 countPrior에 저장된 작업들은 -1
- answer이 추가됐다는 건 실행됐다는 의미이므로 이때 index가 location과 같으면 작업 중지 후 answer 리턴
- 아래 식으로 원형큐 구현
index = (index + 1) % len(priorities)
반응형
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 코딩테스트 고득점 Kit / 스택/큐] 주식가격 (python) (0) | 2024.01.07 |
---|---|
[프로그래머스 코딩테스트 고득점 Kit / 스택/큐] 다리를 지나는 트럭 (python) (0) | 2024.01.06 |
[프로그래머스 코딩테스트 고득점 Kit / 스택/큐] 기능개발 (python) (1) | 2024.01.02 |
[프로그래머스 코딩테스트 고득점 Kit / 스택/큐] 올바른 괄호 (python) (0) | 2024.01.02 |
[프로그래머스 코딩테스트 고득점 Kit / 스택/큐] 같은 숫자는 싫어 (python) (1) | 2023.12.31 |