[programmers] 요격 시스템

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

import java.util.*;

class Solution {
    public int solution(int[][] targets) {
        int answer = 0;

        Arrays.sort(targets, (o1,o2) -> o1[0] - o2[0]);

        PriorityQueue<int[]> pq = new PriorityQueue<>((o1,o2) -> o1[1] - o2[1]);

        for(int[] target : targets){
            if( !pq.isEmpty() && pq.peek()[1] <= target[0]){
                while(!pq.isEmpty()){
                    pq.poll();
                }
                answer++;
            }
            pq.offer(target);
        }

        return answer + ( pq.isEmpty() ? 0 : 1 );
    }
}

© 2023 Lee. All rights reserved.