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

백준 1931

by 신재권 2022. 7. 5.
package baekjoon.그리디;

import java.util.*;
import java.io.*;

class Main1931 {

   public static void main(String[] args) throws Exception {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      StringTokenizer st;
      int N = Integer.parseInt(br.readLine());
      PriorityQueue<Pair> pq = new PriorityQueue<>();
      for (int i = 0; i < N; i++) {
         st = new StringTokenizer(br.readLine());
         pq.add(new Pair(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
      }
      int endTime = 0;
      int ans = 0;
      while (!pq.isEmpty()) {
         Pair p = pq.poll();
         if (endTime <= p.s) {
            endTime = p.f;
            ans++;
         }
      }
      System.out.println(ans);

   }

   private static class Pair implements Comparable<Pair> {
      int s, f;

      public Pair(int start, int finish) {
         this.s = start;
         this.f = finish;
      }

      public int compareTo(Pair o1) {
         if (this.f == o1.f) {
            return this.s - o1.s;
         }
         return this.f - o1.f;
      }
   }
}



 

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

백준11399  (0) 2022.07.07
백준 2156  (0) 2022.07.05
백준 1541  (0) 2022.07.04
백준 10817  (0) 2022.07.02
백준 1715  (0) 2022.07.01