robert-min / dev-blog

김민준 - project 정리한 repo입니다.
0 stars 0 forks source link

231114 - 힙(Heap) : 우선순위 힙 기준 설정, 이중우선순위 큐 #108

Open robert-min opened 1 year ago

robert-min commented 1 year ago

https://school.programmers.co.kr/learn/courses/30/lessons/42626?language=java#

더 맵게

import java.util.*;

class Solution {
    public int solution(int[] scoville, int K) {
        int answer = 0;
        // 1
        PriorityQueue<Integer> Q = new PriorityQueue<>();
        for (int s : scoville) {
            Q.add(s);
        }

        // 2
        while (Q.size() >= 2 && Q.peek() < K) {
            int a = Q.poll();
            int b = Q.poll();
            // 3
            Q.add(a + 2 * b);
            answer++;
        }

        //4
        if (Q.peek() < K) {
            return -1;
        } 
        return answer;
    }
}
robert-min commented 1 year ago

class Solution {

public int solution(int[][] jobs) {
    int answer = 0;
    // 1 : 요청시간 기준을 정렬
    Arrays.sort(jobs, ((o1, o2) -> {
        return o1[0] - o2[0];
    }));

// System.out.println(Arrays.toString(jobs));

    // 2 : 진행시간 기준으로 출력하는 우선순위 큐
    int time = 0;
    int sum = 0;
    PriorityQueue<int[]> PQ = new PriorityQueue<>((o1, o2) -> o1[1] - o2[1]);

    int cnt = 0;
    int jobsIdx = 0;

    while (cnt < jobs.length) {

        while (jobsIdx < jobs.length && jobs[jobsIdx][0] <= time) {
            PQ.add(jobs[jobsIdx++]);
        }

        // 3 : PQ가 들어와있지 않는 경우 : time 조절
        if (PQ.isEmpty()) {
            time = jobs[jobsIdx][0];
        } else {
            int[] tmp = PQ.poll();
            sum += time + tmp[1] - tmp[0];
            time += tmp[1];
            cnt++;
        }

    }
    // 4 : 평균 계산 소수점 버림
    answer = (int)Math.floor(sum / jobs.length);

    return answer;

}

robert-min commented 1 year ago

https://school.programmers.co.kr/learn/courses/30/lessons/42628?language=java

이중우선순위 큐

class Solution {
public int[] solution(String[] operations) { // 1 PriorityQueue minQ = new PriorityQueue<>(); PriorityQueue maxQ = new PriorityQueue<>((o1, o2) -> (o2 - o1));

    for (String op : operations) {
        // 2
        if (op.startsWith("I")) {
            int n = Integer.parseInt(op.substring(2));
            minQ.add(n);
            maxQ.add(n);
        } else {
            // 3
            if (op.equals("D 1") && !maxQ.isEmpty()) {
                int delete = maxQ.poll();
                minQ.remove(delete);
            } else if (op.equals("D -1") && !minQ.isEmpty()) {
                int delete = minQ.poll();
                maxQ.remove(delete);
            }
        }
    }
    // 4
    int[] answer = new int[2];
    if (!maxQ.isEmpty() && !minQ.isEmpty()) {
        answer[0] = maxQ.poll();
        answer[1] = minQ.poll();
    }

    return answer;

}

}

robert-min commented 1 year ago

https://school.programmers.co.kr/learn/courses/30/lessons/42884#

단속카메라

import java.util.*;

class Solution {
    public int solution(int[][] routes) {

        // 1 : 차량이 나가는 지점으로 정렬
        Arrays.sort(routes, ((o1, o2) -> o1[1] - o2[1]));
        // System.out.println(Arrays.toString(routes[routes.length-1]));

        // 2
        int cam = Integer.MIN_VALUE;
        int answer = 0;
        for (int[] route : routes) {
            // 캠이 설치된 위치보다 다음 루트가 크면 새로운 캠을 설치
            if (cam < route[0]) {
                cam = route[1];
                answer++;
            }
        }

        return answer;
    }
}