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
'Algorithm' 카테고리의 다른 글
백준 1417 국회의원 선거 (0) | 2023.07.17 |
---|---|
백준 2738 행렬 덧셈 (0) | 2023.07.16 |
백준 7662 이중 우선순위 큐 (0) | 2023.07.14 |
백준 15903 카드 합체 놀이 (0) | 2023.07.13 |
백준 2447 별찍기10 (0) | 2023.07.12 |