프로그래머스. 야근지수

2024. 3. 22. 10:26코딩 테스트/프로그래머스

https://school.programmers.co.kr/learn/courses/30/lessons/12927

 

프로그래머스

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

programmers.co.kr

 

전체 코드

한승협님 코드

def solution(n, works):
    
    works.sort()
    while n != 0 and sum(works) != 0:
        max_ = max(works)
        for i in range(len(works)-1, -1, -1):
            if works[i] >= max_:
                works[i] -= 1
                n -= 1
                if n == 0:
                    break
    return sum([i**2 for i in works])

 

내가 짜본 코드 - Heap 사용

def solution(n, works):
    heap = []
    for w in works:
        heapq.heappush(heap, -w)

    while heap and n > 0:
        w = -heapq.heappop(heap)
        w -= 1
        if w > 0:
            heapq.heappush(heap, -w)
        n -= 1

    result = sum(list(map(lambda x: x ** 2, heap)))
    return result