https://programmers.co.kr/learn/courses/30/lessons/42626
코딩테스트 연습 - 더 맵게
매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 만들고 싶습니다. 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 Leo는 스코빌 지수가 가장 낮은 두 개의 음식을 아래와 같
programmers.co.kr
PriorityQueue<Integer> pq = new PriorityQueue<>() : 우선순위큐 선언. 최소힙
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()) : 우선순위큐 선언. 최대힙
import java.util.*;
class Solution {
    public int solution(int[] scoville, int K) {
        int answer = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for(int i=0; i<scoville.length; i++){
            pq.add(scoville[i]);
        }
        
        while(pq.size()>=2 && pq.peek()<K){
            answer++;
            int a=pq.poll();
            int b = pq.poll();
            pq.add(a+2*b);
        }
        if(pq.peek()<K) answer=-1;
        return answer;
    }
}
https://programmers.co.kr/learn/courses/30/lessons/42627
코딩테스트 연습 - 디스크 컨트롤러
하드디스크는 한 번에 하나의 작업만 수행할 수 있습니다. 디스크 컨트롤러를 구현하는 방법은 여러 가지가 있습니다. 가장 일반적인 방법은 요청이 들어온 순서대로 처리하는 것입니다. 예를
programmers.co.kr
Arrays.sort(int[][] jobs, ((o1,o2)->o1[0]-o2[0]) : 0번째 원소를 기준 오름차순
Arrays.sort(int[][] jobs, ((o1,o2)->o2[0]-o1[0]) : 0번째 원소를 기준 내림차순
import java.util.*;
class Solution {
    public int solution(int[][] jobs) {
        int answer = 0;
        int cnt=0;
        int end=0;
        int i=0;
        Arrays.sort(jobs,((o1,o2)->o1[0]-o2[0])); //첫번째 요소 기준 오름차순
        PriorityQueue<int[]> pq = new PriorityQueue<>((o1,o2)->o1[1]-o2[1]);
  
        while(cnt<jobs.length){
            while(i<jobs.length && end>=jobs[i][0]){
                pq.add(jobs[i++]);
            }
            if(!pq.isEmpty()){
                int[] cur = pq.poll();
                end+=cur[1];
                answer+=end-cur[0];
                // System.out.println(end-cur[0]);
                cnt++;
            }else{
                end=jobs[i][0];
            }
        }
        return answer/jobs.length;
    }
}'Algorithm > 자료구조' 카테고리의 다른 글
| [프로그래머스] 코딩테스트 고득점 Kit - 스택/큐 (JAVA) (0) | 2021.10.02 | 
|---|---|
| [프로그래머스] 코딩테스트 고득점 Kit - 해시 (JAVA) (0) | 2021.09.30 | 
| [백준] 2470번 : 두 용액 (C++) (0) | 2021.08.30 | 
| [백준] 5430번 : AC (C++) (0) | 2021.08.23 | 
										
									
										
									
										
									
										
									
댓글