Algorithm

백준 11000 강의실 배정

dalooong 2023. 7. 14. 10:23
 

11000번: 강의실 배정

첫 번째 줄에 N이 주어진다. (1 ≤ N ≤ 200,000) 이후 N개의 줄에 Si, Ti가 주어진다. (0 ≤ Si < Ti ≤ 109)

www.acmicpc.net

문제 코드 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class boj11000 {
    public int solution() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int courses = Integer.parseInt(reader.readLine());
        PriorityQueue<int[]> lectureQueue = new PriorityQueue<>(
                Comparator.comparingInt(o -> o[0])
        );
        for (int i = 0; i < courses; i++) {
            StringTokenizer courseToken = new StringTokenizer(reader.readLine());
            lectureQueue.offer(new int[]{
                    Integer.parseInt(courseToken.nextToken()),
                    Integer.parseInt(courseToken.nextToken())
            });
        }
        // 종료시간을 정렬하기 위한 우선순위 큐
        PriorityQueue<Integer> roomQueue = new PriorityQueue<>();
        // 모든 강의를 확인한다.
        while (!lectureQueue.isEmpty()){
            int[] nextLecture = lectureQueue.poll();
            // 지금 사용중인 강의실 중 가장 빨리 비는 강의실이
            // 나의 시작시간보다 빨리 끝날 경우, 해당 강의실을 사용한다.
            if (!roomQueue.isEmpty() && roomQueue.peek() <= nextLecture[0])
                roomQueue.poll();
            roomQueue.offer(nextLecture[1]);
        }
        return roomQueue.size();
    }

    public static void main(String[] args) throws IOException {
        System.out.println(new boj11000().solution());
    }

}

출력결과

입력
3
1 3
2 4
3 5

출력
2