반응형
프로그래머스
문제: https://school.programmers.co.kr/learn/courses/30/lessons/42627
풀이: https://geunzrial.tistory.com/37
import java.util.Arrays;
import java.util.PriorityQueue;
class Solution {
private int finishedCount = 0; // 처리한 작업
private int time = 0; // 하나의 job이 끝나는 시간
public int solution(int[][] jobs) {
int answer = 0;
Arrays.sort(jobs, (o1, o2) -> {
if(o1[0] <= o2[0]) return -1;
return 1;
});
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> {
if(o1[1] <= o2[1]) return -1;
return 1;
});
int i=0;
while(finishedCount < jobs.length) {
while(i < jobs.length && jobs[i][0] <= time) {
pq.add(jobs[i++]);
}
if(pq.isEmpty()) {
time = jobs[i][0];
}
else {
int[] job = pq.poll();
answer += time - job[0] + job[1];
time += job[1];
finishedCount++;
}
}
return answer/jobs.length;
}
}
Comparator 람다형태
Arrays.sort(jobs, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] <= o2[0]) return -1;
return 1;
}
});
Arrays.sort(jobs, (o1, o2) -> {
if(o1[0] <= o2[0]) return -1;
return 1;
});
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[1] <= o2[1]) return -1;
return 1;
}
});
PriorityQueue<int[]> pq2 = new PriorityQueue<>((o1, o2) -> {
if(o1[1] <= o2[1]) return -1;
return 1;
});
반응형
'알고리즘 > 문제풀이' 카테고리의 다른 글
[정렬] 문자열 압축 (0) | 2022.08.07 |
---|---|
[정렬] 오픈채팅방 (0) | 2022.08.07 |
[이진수] Binary Gap (0) | 2019.04.11 |
[BFS] 아기상어 (두번째) (0) | 2019.04.11 |
[DP] 퇴사 (0) | 2019.04.11 |