본문 바로가기
알고리즘 & 자료구조/백준

백준 1931

by 신재권 2023. 7. 25.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class Main1931 {

   public static void main(String[] args) throws Exception {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int N = Integer.parseInt(br.readLine());

      PriorityQueue<Pair> pq = new PriorityQueue<>();
      for (int i = 0; i < N; i++) {
         StringTokenizer st = new StringTokenizer(br.readLine());
         int start = Integer.parseInt(st.nextToken());
         int end = Integer.parseInt(st.nextToken());

         pq.add(new Pair(start, end));
      }

      int endTime = 0;
      int answer = 0;

      while (!pq.isEmpty()) {
         Pair p = pq.poll();
         if (endTime <= p.start) {
            endTime = p.end;
            answer++;
         }
      }

      System.out.println(answer);
   }

   private static class Pair implements Comparable<Pair> {
      int start, end;

      public Pair(int start, int end) {
         this.start = start;
         this.end = end;
      }

      @Override
      public int compareTo(Pair o) {
         if (this.end == o.end) {
            return this.start - o.start;
         }

         return this.end - o.end;
      }
   }
}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 17615  (0) 2023.07.26
백준 21758  (0) 2023.07.25
백준 1080  (0) 2023.07.24
백준 11501  (0) 2023.07.24
백준 21314  (0) 2023.07.23