import java.util.*;
class Solution {
public int solution(int[][] jobs) {
PriorityQueue<Job> pq = new PriorityQueue<Job>((j1, j2) -> j1.duration - j2.duration);
Arrays.sort(jobs, (o1, o2) -> o1[0] - o2[0]);
int answer = 0;
int time = 0;
int index = 0;
while (index < jobs.length || !pq.isEmpty()) {
while (index < jobs.length && jobs[index][0] <= time) { // 이미 뭔가 실행중...
pq.add(new Job(jobs[index][0], jobs[index][1]));
index++;
}
if (pq.isEmpty()) {
time = jobs[index][0];
} else {
Job job = pq.poll();
answer += (time - job.start + job.duration);
time += job.duration;
}
}
return answer / jobs.length;
}
}
class Job {
int start;
int duration;
public Job(int start, int duration) {
this.start = start;
this.duration = duration;
}
}