한 개의 회의실이 존재하고, 이를 사용하고자하는
N
개의 회의에 대하여 최대 사용할 수 있는 회의 최대 개수를 출력하시오.
[ 입력 ]
11
1 4
3 5
0 6
5 7
3 8
5 9
6 10
8 11
8 12
2 13
12 14
[ 출력 ]
4
[ 힌트 ]
(1,4) (5,7) (8,11) (12,14)를 이용할 수 있다.
1) 끝나는 시간을 기준으로 정렬한다. 만약 겹칠 경우, 시작 시간이 느린 순(소요 시간이 짧은)으로 정렬한다.
2) 이전에 선택된 회의의 끝나는 시간과 이후에 선택될 회의의 시작 시간이 겹치지 않는다면, 해당 회의를 다음 회의로 선택한다.
3) 카운트를 세면 결과를 도출할 수 있다.
public class BaekJoon1931 {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int count = 1;
int N = Integer.parseInt(bf.readLine());
int[][] times = new int[N][2];
for(int i=0;i<N;i++) {
st = new StringTokenizer(bf.readLine());
times[i][0] = Integer.parseInt(st.nextToken());
times[i][1] = Integer.parseInt(st.nextToken());
}
Arrays.sort(times, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[1] == o2[1])
return o1[0]-o2[0];
return o1[1]-o2[1];
}
});
// 끝나는 시간 기준 오름차순 정렬
// 같을 경우, 시작 시간이 느린 순(소요 시간이 짧은)으로 정렬
int now = times[0][1];
for(int i=1;i<N;i++) {
if (times[i][0] >= now) {
now = times[i][1];
count++;
}
}
System.out.println(count);
}
}
너무 당연한건가?