23년 8월 9일 [알고리즘 - sorting & thinking]

sua·2023년 8월 9일
0

알고리즘 가보자고

목록 보기
72/101

인프런 최소 회의실 개수

문제

나의 풀이

import java.util.ArrayList;

public class MinumumRoom {
    public static int solution(int[][] meetings) {
        ArrayList<int[]> list = new ArrayList<>();
        for(int[] x : meetings) {
            list.add(new int[]{x[0], 1}); // 시작 시간은 1
            list.add(new int[]{x[1], 2}); // 끝나는 시간은 2로 구분
        }
        list.sort((a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]); // 시간이 같으면 끝나는 시간을 우선으로 정렬
        int answer = 0, count = 0;
        for(int[] x : list) {
            if(x[1] == 1) { // 시작 시간인 경우
                count++; // 회의실 개수 추가
            } else { // 끝나는 시간인 경우
                count--; // 회의실 개수 감소
            }
            answer = Math.max(answer, count); // 매번 최댓값 확인하기
        }
        return answer;
    }
    public static void main(String[] args) {
        System.out.println(MinumumRoom.solution(new int[][]{{0, 10}, {20, 25}, {5, 15}, {2, 5}}));
        System.out.println(MinumumRoom.solution(new int[][]{{0, 5}, {2, 7}, {4, 5}, {7, 10}, {9, 12}}));
    }
}

스케줄대로 회의가 진행될때 동시에 진행되는 회의가 가장 많은 순간의 회의 개수를 찾으면 된다. 그 개수가 최소 회의실 개수가 된다. 끝나는 시간과 시작하는 시간이 겹치는 경우에는 끝나는 시간을 먼저 처리해준다.

결과

profile
가보자고

0개의 댓글