import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static class Conference implements Comparable<Conference> {
int start;
int end;
public Conference(int start, int end) {
this.start = start;
this.end = end;
}
// 끝나는 시각으로 오름차순 정렬
@Override
public int compareTo(Conference cf) {
if (this.end > cf.end) {
return 1;
} else if (this.end == cf.end) {
return this.start - cf.start;
} else {
return -1;
}
}
}
public static void main(String[] args) throws IOException {
// 입력
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
Conference[] conferences = new Conference[N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
conferences[i] = new Conference(start, end);
}
// 끝나는 시각 오름차순 회의 탐색
Arrays.sort(conferences);
int endTime = conferences[0].end;
int cnt = 1;
for (int i = 1; i < conferences.length; i++) {
Conference conference = conferences[i];
if (endTime <= conference.start) {
cnt++;
endTime = conference.end;
}
}
System.out.println(cnt);
}
}
프로그래머스 호텔 대실
문제와 굉장히 유사하다고 생각했다.
https://school.programmers.co.kr/learn/courses/30/lessons/155651
하지만 큰 차이점이 있는 부분은,
방이 한 개 있고 그 중에서 진행할 수 있는 최대 회의 수 를 구하는 것이라면,
호텔 대실 문제는 회의가 진행될 때 가장 최대로 쓰는 방의 개수 를 구하는 것이었다.
그러나 끝나는 시간을 기준으로 문제를 이해한다는 큰 흐름은 비슷하다.
끝나는 시각을 오름차순 정렬하고, 다음 시작 시각이 현재 끝나는 시간보다 크거나 같으면 회의를 진행한다.
이 문제에서 주목할 만한 점은 Comparable
의 compareTo
메서드를 오버라이딩 하는 방법인데,
어떤 기준으로 sort
를 할 것인지 구현하는 연습을 해보는 점이 좋았다.