알고리즘

백준 1931번 회의실 배정 (JAVA)

박카스마시며코딩 2021. 12. 16. 10:57

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

 

1931번: 회의실 배정

(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.

www.acmicpc.net

 

저는 Priority Queue 를 통해 해당 가장 빨리 끝나는 시간을 찾았습니다. 이렇게해서 처음에 88%에서 틀렸습니다.

다시 문제를 자세히 읽어보니 '회의 시작시간과 끝나는 시간이 같을 수 있는다'라는 문구를 보게되었습니다.

3

1 1

0 1

2 2

이렇게 입력이 들어온다면 1 1 이 먼저 들어가고 0 1 이 나중에 들어가게 되면 0 1이 카운팅 되지 않습니다.

그래서 저는 Priority Queue 우선순위를 끝나는 시간이 같다면 시작 시간이 빠른 것이 먼저 오도록 주었습니다.

 

package BOJ;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_1931 {
    private static class Node{
        int start;
        int end;

        public Node(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine()," ");
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(st.nextToken());
        Queue<Node> q = new PriorityQueue<>((o1,o2)->{
            if(o1.end == o2.end){
                return o1.start - o2.start;
            }else{
                return o1.end - o2.end;
            }
        });
        for(int i = 0 ; i < n ; i++){
            st = new StringTokenizer(br.readLine()," ");
            int a = stoi.apply(st.nextToken());
            int b = stoi.apply(st.nextToken());
            q.offer(new Node(a,b));
        }
        int result = 0;
        int endTime = -1;
        while(!q.isEmpty()){
            Node now = q.poll();
            int start = now.start;
            int end = now.end;
            if(start >= endTime){
                endTime = end;
                result++;
            }
        }
        System.out.println(result);
    }
}