프로그래머스-디스크 컨트롤러

이호영·2022년 4월 9일
0
import java.util.*;
class task{
    int start;
    int time;
    task(int start, int time){
        this.start=start;
        this.time=time;
    }
}

class Solution {
    public int solution(int[][] jobs) {
      int ans=0, i=1 ,end=0;
        int start_now= 0, time=0;
        task t;
        Arrays.sort(jobs, new Comparator<int[]>(){
            @Override
            public int compare(int[] t1, int[] t2){
                if(t1[0]==t2[0])
                    return t1[1]-t2[1];
                else
                     return t1[0]-t2[0];
            }
        });
        Queue<task> minheap=new PriorityQueue(Comparator.comparing((task k)->k.time));
        minheap.add(new task(jobs[0][0], jobs[0][1]));
        end = jobs[0][0];
        while(!minheap.isEmpty())
        {
            t = minheap.poll();
            end+= t.time;
            for(; i < jobs.length; i++)
            {
                if(jobs[i][0] > end)
                    break;
                minheap.add(new task(jobs[i][0], jobs[i][1]));
            }            
            ans += end - t.start;
            if(minheap.isEmpty() && i < jobs.length)
            {
                minheap.add(new task(jobs[i][0], jobs[i][1]));
                end = jobs[i++][0];
            }
        }
        return ans / jobs.length;
    }
}

0개의 댓글