Home Programmers. Disk Controller
Post
Cancel

Programmers. Disk Controller

[Link] https://programmers.co.kr/learn/courses/30/lessons/42627?language=java#


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Arrays;
import java.util.PriorityQueue;

class Solution {
	public int solution(int[][] jobs) {
        Arrays.sort(jobs, (l, r) -> l[0] - r[0]);
        int endT = 0, jobIdx = 0, sum = 0, len = jobs.length;
        PriorityQueue<int[]> pq = new PriorityQueue<int[]>((l,r)->l[1]-r[1]);

        while(jobIdx < len) {
            while(jobIdx < len && endT >= jobs[jobIdx][0]) pq.add(jobs[jobIdx++]);
            if(pq.size() == 0) endT = jobs[jobIdx][0];
            else {
                int[] timeInfo = pq.poll();
                endT = endT + timeInfo[1];
                sum += endT - timeInfo[0];
            }
        }
        while(pq.size() != 0) {
            int[] timeInfo = pq.poll();
            endT = endT + timeInfo[1];
            sum += endT - timeInfo[0];
        }
        return sum / len;
	}
}
This post is licensed under CC BY 4.0 by the author.