안녕하세요. 오늘은 프로그래머스의 디스크 컨트롤러 문제를 풀어보겠습니다.!!
https://programmers.co.kr/learn/courses/30/lessons/42627
import java.util.*;
public class solution {
public int solution(int[][] jobs){
int answer = 0;
int cnt = 0;
int time = 0;
LinkedList<Job> waiting = new LinkedList<>();
PriorityQueue<Job> pQueue = new PriorityQueue<>(new Comparator<Job>() {
@Override
public int compare(Job o1, Job o2) {
return o1.workingTime - o2.workingTime;
}
});
for (int[] arr : jobs) {
waiting.offer(new Job(arr[0], arr[1]));
}
Collections.sort(waiting, new Comparator<Job>() {
@Override
public int compare(Job o1, Job o2) {
return o1.requestTime - o2.requestTime;
}
});
time = waiting.peek().requestTime;
while(cnt < jobs.length) {
while(!waiting.isEmpty() && waiting.peek().requestTime <= time) {
pQueue.offer(waiting.poll());
}
if (!pQueue.isEmpty()) {
Job job = pQueue.poll();
time = time + job.workingTime;
answer = answer+ time - job.requestTime;
cnt++;
} else {
time++;
}
}
return answer/cnt;
}
}
class Job {
int requestTime;
int workingTime;
Job(int requestTime, int workingTime) {
this.requestTime = requestTime;
this.workingTime = workingTime;
}
}