Lewis's Tech Keep

[프로그래머스] 오답 - 디스크 컨트롤러 본문

Java/알고리즘

[프로그래머스] 오답 - 디스크 컨트롤러

Lewis Seo 2021. 1. 27. 01:50

for 구문으로만 해결할 방법이 있을까 계속 찾았는데 아직은 못 찾았다.

 

실패코드

 

더보기
import java.util.*;
class Solution {
    public int solution(int[][] jobs) {
        Arrays.sort(jobs, new Comparator<int[]>() {
            public int compare(int[] o1, int[] o2) {
                if(o1[0] <= o2[0]) {
                    return -1;
                }
                return 1;
            }
        });  
        int answer = 0;
        int currentTime = 0;
        PriorityQueue<Integer> pg = new PriorityQueue();
        Map<Integer, List<Integer>> timeMap = new HashMap();
        for(int i=0;i<jobs.length;i++) {
            pg.add(jobs[i][1]);
            if(timeMap.containsKey(jobs[i][1])) {
                List<Integer> arr = timeMap.get(jobs[i][1]);
                arr.add(jobs[i][0]);
                timeMap.put(jobs[i][1], arr);
            } else {
                List<Integer> arr = new ArrayList();
                arr.add(jobs[i][0]);
                timeMap.put(jobs[i][1], arr);
            }
        }
        
        int pgSize = pg.size();
        for(int j=0;j<pgSize;j++) {
            int workTime = pg.poll();
            List<Integer> startTimeMap = timeMap.get(workTime);
            int startTime = startTimeMap.get(0);
            startTimeMap.remove(0);
            currentTime = currentTime < startTime ? startTime : currentTime;
            currentTime += workTime;
            answer += currentTime - startTime;
            System.out.println(answer);
        }
        
        return (int) (answer/pgSize);
    }
}

'Java > 알고리즘' 카테고리의 다른 글

[프로그래머스] 이중우선순위  (0) 2021.01.29
[프로그래머스] 타겟 넘버  (0) 2021.01.28
[프로그래머스] 기능개발  (0) 2021.01.22
[프로그래머스] 주식가격  (0) 2021.01.21
[General] 캐싱에 관해  (0) 2021.01.12
Comments