Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

hwooo

BOJ (Java) 1931번: 회의실 배정 본문

Study/Algorithm

BOJ (Java) 1931번: 회의실 배정

hwooo 2025. 5. 29. 23:18

 

https://www.acmicpc.net/problem/1931


풀이

선택할 수 있는 회의가 많아지려면, 회의가 일찍 끝나면 된다.

회의의 끝나는 시간을 기준으로 정렬한 후, 앞의 회의가 끝나면 다음 회의를 시작하는 방식으로 풀었다.

시작 시간과 끝나는 시간이 같은 회의도 있기에 끝나는 시간이 같다면 시작 시간이 작은 순으로 정렬했다.


Java 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    private static int[][] times;

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

        times = new int[N][2];
        for (int i = 0; i < N; i++) {
            String[] inputs = bf.readLine().split(" ");
            times[i][0] = Integer.parseInt(inputs[0]);
            times[i][1] = Integer.parseInt(inputs[1]);
        }

        // 끝나는 시간이 빠른 순으로 정렬
        Arrays.sort(times, (a, b) -> {
            if (a[1] == b[1]) {
                return Integer.compare(a[0], b[0]);
            }
            return Integer.compare(a[1], b[1]);
        });

        int finTime = times[0][1];
        int cnt = 1;
        for (int i = 1; i < N; i++) {
            // 현재 회의 종료 시간 < 다음 회의 시작 시간
            if (finTime <= times[i][0]) {
                finTime = times[i][1];
                cnt++;
            }
        }
        System.out.println(cnt);
    }
}

'Study > Algorithm' 카테고리의 다른 글

BOJ (Java) 10942번: 팰린드롬?  (0) 2025.06.02
BOJ (Java) 1197번: 최소 스패닝 트리  (0) 2025.06.02
BOJ (Java) 1987번: 알파벳  (0) 2025.05.27
BOJ (Java) 1062번: 가르침  (0) 2025.05.26
BOJ (Java) 2473번: 세 용액  (0) 2025.05.25