주의할 점
1. 작업요청이 동시에 들어온 경우 ex) [0, 8], [0, 3], [0, 5]
2. 작업이 끝나고 다음 작업이 바로 실행되지 않는 경우 ex) [0,2], [10,2]
3. 현 작업 진행 중에 여러 작업들이 요청된다면 작업 실행 시간이 짧은 순으로 먼저 실행해야 함
import java.util.*;
class Solution {
public int solution(int[][] jobs) {
//소요시간
int answer = 0;
//끝나는 시간
int end = 0;
PriorityQueue<int[]> q = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[0]!=o2[0] ? o1[0]-o2[0] : o1[1]-o2[1];
}
});
PriorityQueue<int[]> readyQ = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]);
for(int i=0; i<jobs.length; i++){
q.add(jobs[i]);
}
int[] now = new int[2];
while(q.size() >0){
if(readyQ.isEmpty()){
//마지막 작업시간보다 늦게 시작되는 경우
if (q.peek()[0] > end ){
now = q.poll();
answer+= now[1];
end = (now[1] + now[0]);
}
//마지막 작업 시간 전에 시작되는 경우
while(q.peek()!= null ){
if(q.peek()[0] <= end){
readyQ.add(q.poll());
}
else break;
}
}
//대기하고 있던 작업들 처리
while(!readyQ.isEmpty()){
now = readyQ.poll();
answer += (end - now[0] + now[1]);
end += now[1];
//현 작업이 진행되고 있던 사이 대기하는 작업들 있나 확인
while(q.peek()!= null ){
if(q.peek()[0]<= end){
readyQ.add(q.poll());
}
else break;
}
}
}
return answer/jobs.length;
}
}
'알고리즘' 카테고리의 다른 글
Comparator의 compareTo (0) | 2023.03.05 |
---|