코딩테스트/프로그래머스

[프로그래머스][Heap] 야근 지수

박소민 2024. 11. 1. 11:49
야근 지수
 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

  • 내 풀이
    • n<= 1,000,000
      • ➡ heapq 사용
    • queue가 비어있는지 확인 안하면 런타임에러!!
import heapq
def solution(n, works):
    #피로도: 시작 시점+(남은일의 작업량)**2
    answer = 0
    queue=[]
    for work in works:
        heapq.heappush(queue,-work)
    while n>=1:
        if not queue:
            break
        tmp=heapq.heappop(queue)
        if tmp!=0:
            heapq.heappush(queue, tmp+1)
        n-=1
    
    for q in queue:
        if q==0:
            continue
        answer+=q**2
    return answer