Programmers

Programmers LV.3 디스크 컨트롤러

Developer Garam.Choi 2020. 12. 29. 15:56
 @org.junit.Test
    public void diskController(){
        int[][] jobs ={{0, 10}, {2, 12}, {9, 19}, {15, 17}};

       /*
       int[][] jobs = {{0, 3}, {1, 9}, {2, 6}};
        int[][] jobs ={{0, 10}, {2, 10}, {9, 10}, {15, 2}};


        int[][] jobs ={{0, 3}, {1, 9}, {2, 6}};
        int[][] jobs ={{0, 1}};
        int[][] jobs ={{1000, 1000}};
        int[][] jobs ={{0, 1}, {0, 1}, {0, 1}};
        int[][] jobs ={{0, 1}, {0, 1}, {0, 1}, {0, 1}};
        int[][] jobs ={{0, 1}, {1000, 1000}};
        int[][] jobs ={{100, 100}, {1000, 1000}};
        int[][] jobs ={{10, 10}, {30, 10}, {50, 2}, {51, 2}};
        int[][] jobs ={{0, 3}, {1, 9}, {2, 6}, {30, 3}};
        */


        List<Integer> jobId = new ArrayList<>();
        for(int a=0; a<jobs.length; a++) jobId.add(a);

        Collections.sort(jobId, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                if (jobs[o1][1] == jobs[o2][1]) {
                    return Integer.compare(jobs[o2][1],jobs[o1][1]);
                }
                else {
                    return Integer.compare(jobs[o1][1],jobs[o2][1]);
                }
            }
        });

        int requestStartTime =0;
        int sumTime = 0;

        while (jobId.size() > 0){
            for(int b=0; b<jobId.size(); b++){
                int[] jobValue = jobs[jobId.get(b)];
                int requestTime = jobValue[0];
                int complateTime = jobValue[1];

                if(requestStartTime >= requestTime){
                    requestStartTime += complateTime ;
                    sumTime += requestStartTime - requestTime;;

                    jobId.remove(b);
                    break;
                }

                if(b == jobId.size() -1){
                    requestStartTime++;
                }

                System.out.println("requestStartTime ="+requestStartTime);
                System.out.println("sumTime ="+sumTime);
            }
        }


        int answer =  (int) Math.ceil((double) (sumTime /jobs.length) );
        System.out.println(answer);


    }

 

 

지문을 읽을때마다 느끼는거지만 직관적으로 읽고, 코테를 보는게 도움이 될꺼같다.