less than 1 minute read

1. 개요

2. 풀이

def solution(n, cores):
    # 0초에 core의 갯수만큼 처리할수 있다.
    n -= len(cores)
    left = 0
    time = right = max(cores) * n
    
    # 모든 작업을 처리할수 있는 시간을 이진탐색으로 구한다.
    while left <= right:
        mid = (left + right) // 2
        capacity = sum(mid//c for c in cores)

        if capacity >= n:
            time = mid
            right = mid - 1
        else:
            left = mid + 1

    # 가장 마지막에 처리하는 cpu를 구해야 하므로 time-1만 적용한다.
    n -= sum((time-1) // c for c in cores)

    # 앞에 있는 코어부터 채워가며 가장 마지막으로 채워진 코어번호를 반환하면 된다.
    for i in range(len(cores)):
        if time % cores[i] == 0:
            n -= 1
            if n == 0:
                return i + 1

Leave a comment