https://campus.programmers.co.kr/tryouts/185299/challenges
• 여러 개의 작업이 있다.
• 각 작업은 요청 시점과 작업 소요 시간을 가지고 있다.
• 이 작업들을 처리할 때 평균 응답 시간을 최소화하고 싶다.
응답 시간 = 작업이 끝난 시점 - 요청 시점
1. 작업 요청 시간이 빠른 순으로 정렬
2. 현재 시점 기준으로, 요청이 들어온 작업들 중에서 소요 시간이 가장 짧은 작업부터 처리
3. 작업이 끝난 시간 기준으로 계속 갱신하면서 큐에 넣고 처리
import java.util.*;
class Solution {
public int solution(int[][] jobs) {
Arrays.sort(jobs, (j1, j2) -> j1[0] - j2[0]);
Queue<int[]> pq = new PriorityQueue<>((j1, j2) -> j1[1] - j2[1]);
int currentTime = 0;
int completedJobs = 0;
int totalResponseTime = 0;
int jobIndex = 0;
while (completedJobs < jobs.length) {
while (jobIndex < jobs.length && jobs[jobIndex][0] <= currentTime) {
pq.add(jobs[jobIndex]);
jobIndex++;
}
if (!pq.isEmpty()) {
int[] job = pq.remove();
currentTime += job[1];
totalResponseTime += currentTime - job[0];
completedJobs++;
} else {
currentTime = jobs[jobIndex][0];
}
}
return totalResponseTime / jobs.length;
}
}