쥬로그
Programmers 코딩테스트 고득점 Kit - 힙 본문
반응형
더 맵게
import java.util.*;
class Solution {
public int solution(int[] scoville, int K) {
int answer = 0;
PriorityQueue<Integer> minQueue = new PriorityQueue<>();
for(int s : scoville){
minQueue.add(s);
}
while(minQueue.peek() < K){
int a = minQueue.remove();
if(minQueue.isEmpty()){
answer = -1;
break;
}
int b = minQueue.remove();
int c = a + b * 2;
minQueue.add(c);
answer++;
}
return answer;
}
}
더보기
Reference )
디스크 컨트롤러
import java.util.*;
class Solution {
class Task{
int point;
int time;
public Task(int point, int time){
this.point = point;
this.time = time;
}
}
public int solution(int[][] jobs) {
// 대기 큐
PriorityQueue<Task> waitQue = new PriorityQueue(new Comparator<Task>(){
@Override
public int compare(Task t1, Task t2){
return t1.point - t2.point;
}
});
for(int[] job : jobs){
waitQue.add(new Task(job[0], job[1]));
}
// 작업 큐
PriorityQueue<Task> workQue = new PriorityQueue(new Comparator<Task>(){
@Override
public int compare(Task t1, Task t2){
return t1.time - t2.time;
}
});
int cnt = 0;
int sum = 0;
int time = 0;
while(cnt < jobs.length){
while(!waitQue.isEmpty() && time >= waitQue.peek().point){
workQue.offer(waitQue.poll());
}
if(!workQue.isEmpty()){
Task t = workQue.poll();
sum += t.time + (time - t.point);
time += t.time;
cnt++;
} else {
time++;
}
}
return sum/cnt;
}
}
반응형
'Algorithm > Programmers' 카테고리의 다른 글
Programmers 코딩테스트 고득점 Kit - 스택/큐 (0) | 2022.06.02 |
---|---|
Programmers 코딩테스트 고득점 Kit - 정렬 (0) | 2022.06.01 |
Programmers 코딩테스트 고득점 Kit - 해시 (0) | 2022.06.01 |
Programmers SQL 고득점 Kit - String, Date (MySQL) (0) | 2022.05.31 |
Programmers SQL 고득점 Kit - JOIN (MySQL) (0) | 2022.05.31 |