[백준] 1931. 회의실 배정

장철현·2023년 11월 21일
0

백준

목록 보기
22/80

링크

1931. 회의실 배정

문제

풀이

이 문제는 끝나는 시간 기준으로 오름차순 정렬을 했고 만약 끝나는 시간이 같다면 시작 시간을 기준으로 오름차순을 수행했다.

코드

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

class Node implements Comparable<Node>{
    int start;
    int end;

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

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

        return this.end - o.end;
    }

}


public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int testCase = Integer.parseInt(br.readLine());

        Queue<Node> pq = new PriorityQueue<>();
        for(int t=0;t<testCase;t++){
            String[] arr = br.readLine().split(" ");

            pq.add(new Node(Integer.parseInt(arr[0]), Integer.parseInt(arr[1])));
        }

        int time = 0;
        int count = 0;

        while(!pq.isEmpty()){
            Node element = pq.poll();

            if(element.start >= time){
                time = element.end;
                count++;
            }
        }

        System.out.println(count);


    }

}

0개의 댓글