백준 1931 - 회의실 배정 (자바)

남현·2025년 12월 9일

백준

목록 보기
12/16

문제

풀이

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[][] arr = new int[N][2];

        for(int i=0; i<N; i++) {
            arr[i][0] = sc.nextInt(); // 시작
            arr[i][1] = sc.nextInt(); // 종료
        }
        sc.close();

        Arrays.sort(arr, (a,b) -> {
            if(a[1] == b[1]) return a[0]-b[0];
            return a[1]-b[1];
        });

        int end = 0;
        int count = 0;
        for(int[] m : arr) {
            if(m[0] >= end) {
                count++;
                end = m[1];
            }
        }
        System.out.println(count);
    }
}

그리디 알고리즘
가장 빨리 끝나는 회의순으로 정렬
끝나는 시간이 같으면 시작 시간이 빠른 순으로 정렬

profile
백엔드 호소인

0개의 댓글