Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1/31 restApi 재정리 + 디스크 컨트롤러 #48

Open
skarltjr opened this issue Jan 31, 2021 · 0 comments
Open

1/31 restApi 재정리 + 디스크 컨트롤러 #48

skarltjr opened this issue Jan 31, 2021 · 0 comments
Labels

Comments

@skarltjr
Copy link
Owner

package algo;


import java.util.*;


public class Solution {
    static int total = 0;
    public int solution(int[][] jobs) {
        int answer = 0;
        int count = 0; // 수행된 작업 수
        int endPoint = 0; // 하나의 작업이 끝날 때 마지막 포인트
        // jobs는  [] - 작업이 요청되는 시점 [] - 작업의 소요시간
        // 1. 우선순위 큐에 순서대로 담아서 실행
        // 2. 진행 범위내에서 시작하는것들 큐에 담고 다음에 실행
        // 3. 이 때 여러개가 담겼다면 그 중 작업소요시간이 최소인 순으로 실행
        // 4. 만약 2번에 해당하지않으면 쉬었다가 다시


        // 요청시간순으로 정렬
        Arrays.sort(jobs, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[0];
            }
        });
        // 큐에 들어온놈들은 수행시간순으로 정렬
        PriorityQueue<int[]> q = new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1]-o2[1];
            }
        });
        boolean[] visited = new boolean[jobs.length];

        q.add(jobs[0]);
        visited[0] = true;
        endPoint = jobs[0][0];
        // 첫번째랑 시작은같은데 종료시간이 다른애들도 넣어줘야한다
        for (int i = 1; i < jobs.length; i++) {
            if (jobs[i][0] == jobs[0][0]) {
                q.add(jobs[i]);
                visited[i] = true;
            } else {
                break;
            }
        }

        while (count < jobs.length)
        {
            int[] poll = q.poll();
            total += (endPoint+poll[1]-poll[0]); // 총 수행시간에 더해주고
            endPoint += poll[1];
            count++; // 수행된 개수 추가
            System.out.println(total);

            for (int i = 0; i < jobs.length; i++) {
                if (visited[i] == false && jobs[i][0] < endPoint)
                {
                    q.add(jobs[i]);
                    visited[i] = true;
                }
            }

            // 가장중요한. 만약에 0~~20까지는 쭉 진행되다가 갑자기 [500,5]가 나타나면 20~500까지 쉬다가 시작
            // 근데 위에서 total += (total + poll[1]-poll[0])이거하면 poll[0]이 500인데 딱봐도 -500하면 에러
            // 쉬었을 때
            if (q.isEmpty())
            {
                for (int i = 0; i < jobs.length; i++)
                {
                    if (visited[i] == false)
                    {
                        q.add(jobs[i]);
                        visited[i] = true;
                        endPoint = jobs[i][0];
                        break;
                    }
                }
            }


        }


        return (int)total/ jobs.length;
    }
}



@skarltjr skarltjr added the Daily label Jan 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant