Home 백준 회의실 배정 (1931)
Post
Cancel

백준 회의실 배정 (1931)

백준 회의실 배정 (1931)

백준 회의실 배정

그리디 문제이다.

커스텀 정렬을 이용하여 문제를 해결하였다.

스크린샷 2022-01-29 오전 2 02 40

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;  
import java.util.*;  
  
class Metting implements Comparable<Metting>{  
    public int start;  
    public int end;  
  
    public Metting(int start, int end) {  
        this.start  = start;  
        this.end = end;  
    }  
  
    // 이른 종료시간 우선 정렬  
  @Override  
    public int compareTo(Metting mt) {  
        return (this.end == mt.end) ? this.start - mt.start : this.end - mt.end;  
    }  
}  
  
public class Main {  
  
    public static void main(String[] args) throws NumberFormatException, IOException {  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        Metting rooms[] = new Metting[Integer.parseInt(br.readLine())];  
  
        int count = 0;  
  
        for(int i=0; i<rooms.length; i++) {  
            StringTokenizer st = new StringTokenizer(br.readLine());  
  
            rooms[i] = new Metting(Integer.parseInt(st.nextToken()),  
                    Integer.parseInt(st.nextToken()));  
        }  
  
        int time = 0; // 현재 시간  
  
        Arrays.sort(rooms);  
  
        for(int i=0; i<rooms.length; i++) {  
            if(time <= rooms[i].start) {   // 회의실을 사용할 수 있으면    
			   count ++;  // 사용할 수 있는 회의 수 증가   
			   time =  rooms[i].end;  // 현재시간을 회의 끝나는 시간으로  
	         }  
        }  
  
  
        System.out.println(count);  
  
    }  
  
}
This post is licensed under CC BY 4.0 by the author.