-
Notifications
You must be signed in to change notification settings - Fork 0
/
meetingGreedy.java
38 lines (35 loc) · 1.24 KB
/
meetingGreedy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class meetingGreedy {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int meetingCount = Integer.parseInt(br.readLine());
int[][] array = new int[meetingCount][2];
for (int i=0; i<meetingCount; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
array[i][0] = Integer.parseInt(st.nextToken());
array[i][1] = Integer.parseInt(st.nextToken());
}
Arrays.sort(array, (o1, o2) -> {
if (o1[1]==o2[1]){
return Integer.compare(o1[0],o2[0]);
}
else{
return Integer.compare(o1[1],o2[1]);
}
});
int max = 1;
int maxIndex = array[0][1];
for (int i=1; i<meetingCount; i++){
if (array[i][0] >= maxIndex) {
if(array[i][0] == array[i][1]) maxIndex = array[i-1][1];
else maxIndex = array[i][1];
max++;
}
}
System.out.println(max);
}
}