[노잼]1931

최은창·2024년 5월 2일
post-thumbnail

문제

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

해설

Arrays.sort(room, new Comparator<int[]>(){
            public int compare(int[] a, int[] b){
                if(a[1] == b[1]) return a[0] - b[0];
                return a[1]-b[1];
            }
        });

A를 기준으로 정렬 한 후 같은 값이 존재할 경우 A기준 상태에서 B 정렬을 하는 코드이다.

이는 익명 클래스 작업이 필요하므로 익명 클래스를 선행해보자

이 문제에서는 회의 끝나는 시간별로 정렬을 하였고 끝나는 시간이 같은 경우 시작 시간 기준으로 정렬을 해주었다. 그래야 시작과 동시에 끝나는 회의와 시작시간이 다른 회의를 가져갈 수 있다.

코드


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

public class J1931 {

    public static void main(String[] args)throws IOException {
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

        int index = Integer.parseInt(buffer.readLine());
        int[][] room = new int[index][2];

        for(int i = 0; i< index; i++){
            String[] input = buffer.readLine().split(" ");
            int start = Integer.parseInt(input[0]);
            int end = Integer.parseInt(input[1]);

            room[i][0] = start;
            room[i][1] = end;
        }

        Arrays.sort(room, new Comparator<int[]>(){
            public int compare(int[] a, int[] b){
                if(a[1] == b[1]) return a[0] - b[0];
                return a[1]-b[1];
            }
        });
        int cnt=0;
        int end = -1;
        for(int i = 0; i < index; i++){
            if( room[i][0]>=end){
                end = room[i][1];
                cnt++;
            }
        }
        System.out.println(cnt);

    }
}
profile
비슷한 어려움을 겪는 누군가에게 도움이 되길

0개의 댓글