240222 디스크 컨트롤러

Jongleee·2024년 2월 22일
0

TIL

목록 보기
502/737
public int solution(int[][] jobs) {
	Arrays.sort(jobs, (o1, o2) -> o1[0] - o2[0]);

	PriorityQueue<int[]> minHeap = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]);

	int time = 0;
	int totalResponseTime = 0;
	int jobsIndex = 0;
	int count = 0;

	while (count < jobs.length) {
		while (jobsIndex < jobs.length && jobs[jobsIndex][0] <= time) {
			minHeap.add(jobs[jobsIndex++]);
		}

		if (minHeap.isEmpty()) {
			time = jobs[jobsIndex][0];
		} else {
			int[] currentJob = minHeap.poll();
			totalResponseTime += (time + currentJob[1] - currentJob[0]);
			time += currentJob[1];
			count++;
		}
	}

	return totalResponseTime / jobs.length;
}

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

0개의 댓글