- 난이도: Lv3
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/42627
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/3/디스크 컨트롤러
풀이 시간 : 1시간 25분
import java.util.*;
class Solution {
public int solution(int[][] jobs) {
Arrays.sort(jobs, (a, b) -> Integer.compare(a[0], b[0]));
PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
int time = 0;
int total = 0;
int count = 0;
int index = 0;
while (count < jobs.length) {
while (index < jobs.length && jobs[index][0] <= time) {
pq.offer(jobs[index++]);
}
if (pq.isEmpty()) {
time = jobs[index][0];
} else {
int[] current = pq.poll();
time += current[1];
total += time - current[0];
count++;
}
}
int answer = total / jobs.length;
return answer;
}
}