최소 회의실 개수

Seongjin Jo·2023년 9월 29일
0

Baekjoon

목록 보기
50/51
post-thumbnail

문제

풀이

import java.util.*;

// 최소 회의실 개수
public class ex7 {

    static int n;
    static int[][] meetings;
    static int answer=1;

    public static void solution(){
        Arrays.sort(meetings, (a,b) -> a[0]==b[0] ? a[0]-b[0] : a[1]-b[1]);

        // 회의시간이 가장 빠르게 끝나는 순으로 정렬
        // 2 5
        // 0 10
        // 5 15
        // 12 25
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        pq.offer(meetings[0][1]);

        // pq : 5
        for(int i=1; i<meetings.length; i++){
            if(!pq.isEmpty() && pq.peek()<=meetings[i][0]){
                pq.poll();
            }
            pq.offer(meetings[i][1]);
            answer = Math.max(answer,pq.size());
        }

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        meetings = new int[n][2];
        for(int i=0; i<n; i++){
            meetings[i][0] = sc.nextInt();
            meetings[i][1] = sc.nextInt();
        }
        solution();
        System.out.println(answer);
    }
}

시작 시간 , 종료 시간 기준으로 최소로 사용되는 회의실 개수를 구할 때는 무조건 해야하는 것들이 있다.

    1. 무조건 종료시간 기준으로 오름차순 정렬.
    1. PriorityQueue를 이용해서 최소회의실 개수를 구해야한다.
    PriorityQueue<Integer> pq = new PriorityQueue<>();
     pq.offer(meetings[0][1]);
    
     for(int i=1; i<meetings.length; i++){
               if(!pq.isEmpty() && pq.peek()<=meetings[i][0]){
                   pq.poll();
               }
               pq.offer(meetings[i][1]);
               answer = Math.max(answer,pq.size());
     }

암기 필수.
미팅의 시작시간보다 작은 회의실이 있으면 빼주고, 아니면 계속 pq에 미팅의 종료시간을 접어 넣어준다.

0개의 댓글