fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) throws IOException {
  9. int n = Integer.parseInt(bf.readLine());
  10. Time[] times = new Time[n];
  11.  
  12. for(int i = 0 ;i<n;i++){
  13. st = new StringTokenizer(bf.readLine());
  14. times[i] = new Time(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
  15. }
  16.  
  17. Arrays.stream(times).sorted(Comparator.comparing(o -> o.end));//끝난 시간 기준으로 오름차순 정렬
  18.  
  19. int count = (times.length == 0) ? 0:1;
  20.  
  21. int j=0;
  22. for(int i=1; i<times.length;i++){
  23. if(times[j].end<=times[i].start){// 시작 시간이 끝난 시간보다 같거나 크면
  24. count++;
  25. j=i;
  26. }
  27. }
  28. System.out.println(count);
  29. }
  30. static class Time{
  31. int start;
  32. int end;
  33.  
  34. public Time(int start, int end) {
  35. this.start = start;
  36. this.end = end;
  37. }
  38. }
  39. }
  40.  
  41.  
Success #stdin #stdout 0.08s 50724KB
stdin
2
2 2
1 2
stdout
1