Skip to content

Commit 3a7ecf2

Browse files
authored
Merge pull request #495 from AlgorithmWithGod/03do-new30
[20250718] BOJ / G5 / 강의실 배정 / 신동윤
2 parents d4319da + 6259c7f commit 3a7ecf2

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
static int N;
7+
static class Lecture {
8+
int start;
9+
int end;
10+
11+
public Lecture(int start, int end) {
12+
this.start = start;
13+
this.end = end;
14+
}
15+
}
16+
public static void main(String[] args) throws IOException {
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
N = Integer.parseInt(br.readLine());
19+
20+
Lecture[] lectures = new Lecture[N];
21+
22+
for (int i = 0; i < N; i++) {
23+
StringTokenizer st = new StringTokenizer(br.readLine());
24+
Lecture lecture = new Lecture(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
25+
lectures[i] = lecture;
26+
}
27+
28+
// 시작 시간 기준 오름차순 강의 정렬
29+
Arrays.sort(lectures, new Comparator<Lecture>() {
30+
@Override
31+
public int compare(Lecture o1, Lecture o2) {
32+
return Integer.compare(o1.start, o2.start);
33+
}
34+
});
35+
36+
// 끝나는 시간 기준 오름차순 우선순위 큐
37+
PriorityQueue<Lecture> pq = new PriorityQueue<>(new Comparator<Lecture>() {
38+
@Override
39+
public int compare(Lecture o1, Lecture o2) {
40+
return Integer.compare(o1.end, o2.end);
41+
}
42+
});
43+
44+
for (Lecture current : lectures) {
45+
if (!pq.isEmpty() && pq.peek().end <= current.start) {
46+
pq.poll(); // 기존 강의실
47+
}
48+
pq.offer(current);
49+
}
50+
System.out.println(pq.size());
51+
br.close();
52+
}
53+
}
54+
```

0 commit comments

Comments
 (0)