240122 선입 선출 스케줄링

Jongleee·2024년 1월 22일
0

TIL

목록 보기
475/739
public int solution(int n, int[] cores) {
	int left = -1;
	int right = 100000;
	left = findLeft(n, cores, left, right);
	if (left == -1)
		return 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 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;
}

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

0개의 댓글