230116 선입선출 스케줄링

Jongleee·2023년 1월 16일
0

TIL

목록 보기
157/576

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


    public static int solution(int n, int[] cores) {
        int left = -1;
        int right = 100000;
        left = findLeft(n, cores, left, right);
        //이분 탐색을 통해 처리된 작업의 갯수가 n이 되기 직전인 시간 left를 찾아줌
        if (left == -1)
            return n;
        //left의 값이 -1인 경우 0시간에 모든 작업을 처리할 수 있으므로 n번째에서 마지막 n개를 처리
        int cnt = cores.length;
        for (int i = 0; i < cores.length; i++)
            cnt += left / cores[i];
        for (int i = 0; i < cores.length; i++) {
            if ((left + 1) % cores[i] == 0)
                cnt++;
            if (cnt == n)
                return i + 1;
        }
        return 0;

    }

    private static int findLeft(int n, int[] cores, int left, int right) {
        while (left + 1 < right) {
            int mid = (left + right) / 2;
            int cnt = cores.length;
            if (mid > 0) {
                for (int i = 0; i < cores.length; i++) {
                    cnt += mid / cores[i];
                }
            }
            if (cnt < n)
                left = mid;
            else
                right = mid;
        }
        return left;

    }

0개의 댓글