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;
}
}
'알고리즘 공부 > programmers' 카테고리의 다른 글
Programmers 43236: 징검다리 (0) | 2020.12.26 |
---|---|
Programmers 12907: 거스름돈 (0) | 2020.12.24 |
Programmers 68937: 트리 트리오 중간값 (0) | 2020.12.20 |
Programmers 64063: 호텔 방배정 (0) | 2020.12.18 |
programmers 60060: 가사 검색 (0) | 2020.08.30 |