in.add()
[1931] 회의실 배정 본문
문제 링크 : https://www.acmicpc.net/problem/1931
1931번: 회의실 배정
(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.
www.acmicpc.net
문제 해결 방법
우선 끝나는 시간이 빠른 순서로 정렬했다. 끝나는 시간이 같다면 빨리 시작하는 회의가 우선이다.
처음부터 보면서 끝난 시간을 last 변수에 넣고 그 이후에 시작하는 회의가 있다면 last를 갱신해준다.
갱신할 때 answer++
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[][] time = new int[N][2];
for(int i = 0; i < N; i++) {
time[i][0] = sc.nextInt();
time[i][1] = sc.nextInt();
}
Arrays.sort(time, (o1, o2) -> {
if(o1[1] == o2[1]) {
return o1[0] - o2[0];
}
return o1[1] - o2[1];
});
int answer = 0;
int last = 0;
for(int i = 0; i < N; i++) {
if(last <= time[i][0]) {
last = time[i][1];
answer++;
}
}
System.out.println(answer);
}
}
'Algorithm > BOJ' 카테고리의 다른 글
[2206] 벽 부수고 이동하기 (0) | 2021.10.11 |
---|---|
[15961] 회전 초밥 (0) | 2021.10.06 |
[1194] 달이 차오른다, 가자. (0) | 2021.09.29 |
[1697] 숨바꼭질 (0) | 2021.09.28 |
[16928] 뱀과 사다리 게임 (0) | 2021.09.23 |
Comments