가희의자기개발블로그
백준 1931번 <회의실배정> 그리디알고리즘 본문
반응형
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int totalA = scanner.nextInt();
int[][] arr = new int[totalA][2];
for (int i = 0; i < totalA; i++) {
arr[i][0] = scanner.nextInt();
arr[i][1] = scanner.nextInt();
}
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[1] == o2[1]) {
return o1[0] - o2[0];
} else {
return o1[1] - o2[1];
}
}
});
int max = 1;
int temp = arr[0][1];
for (int i = 1; i < arr.length; i++) {
if (temp <= arr[i][0]) {
max++;
temp = arr[i][1];
}
}
System.out.println(max);
}
}
런타임에러, 시간초과 때문에 계속 고민했다. 다음 회의시간 배정하는데서 그냥 for문으로 쓸수 있는걸 이중 for문으로 돌려버려서 시간 복잡도가 O(n2)이 되어서 시간초과 에러가 났었다. 그걸 해결하니까 풀렸다.
반응형
'프로그래밍 언어 > 알고리즘' 카테고리의 다른 글
백준 2875번 <대회 or 인턴> 그리디 알고리즘 (0) | 2020.07.06 |
---|---|
백준 1541 <잃어버린 괄호> 그리디 알고리즘 (0) | 2020.07.02 |
10610 백준 30 <그리디 알고리즘> (0) | 2020.06.24 |
백준 2217번 로프 <그리디알고리즘> (0) | 2020.06.23 |
백준 5585 :거스름돈 : 그리디알고리즘 (0) | 2020.06.23 |
Comments