본문으로 바로가기

[baekjoon]회의실 배정 JAVA

category 코딩테스트/baekjoon 2022. 11. 24. 15:32

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

 

1931번: 회의실 배정

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

www.acmicpc.net

 

이 문제는 주어진 회의시간을 정렬하는 게 핵심이다.

먼저 1) 끝나는 시간 기준으로 정렬 한 후 2) 시작 시간 기준으로 정렬을 한다. 

 

이렇게 정렬이 된 후 for 문을 이용해서 차례대로 카운트를 하기만 하면 된다. 

 
import java.util.*;

class Time {
	int start;
	int end;
	public Time(int start, int end) {
		super();
		this.start = start;
		this.end = end;
	}
	
}
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		List<Time> list = new ArrayList<Time>();
		for(int i =0; i < n; i++) {
			int s = sc.nextInt();
			int e = sc.nextInt();
			list.add(new Time(s, e));
		}
		
		
		Collections.sort(list, new Comparator<Time>() {

			@Override
			public int compare(Time o1, Time o2) {
				if(o1.end == o2.end) {
					return o1.start - o2.start;
				}
				return o1.end - o2.end;
			}
			
		});
		
		int cnt = 0;
		int end_time = 0;
		
		for(int i = 0; i < n; i++) {
			Time t = list.get(i);
			if(end_time <= t.start) {
				end_time = t.end;
				cnt++;
			}
		}
		
		System.out.println(cnt);
	}
}

'코딩테스트 > baekjoon' 카테고리의 다른 글

[beakjoon] 드래곤 커브 JAVA  (0) 2022.11.10
[baekjoon] 로봇 청소기 JAVA  (0) 2022.11.09
[beakjoon]퇴사 JAVA  (0) 2022.11.08
[baekjoon] 주사위 굴리기 JAVA  (0) 2022.11.08