π‘ Info
λ΄μ©
ν κ°μ νμμ€μ΄ μλλ° μ΄λ₯Ό μ¬μ©νκ³ μ νλ Nκ°μ νμμ λνμ¬ νμμ€ μ¬μ©νλ₯Ό λ§λ€λ €κ³ νλ€. κ° νμ Iμ λν΄ μμμκ°κ³Ό λλλ μκ°μ΄ μ£Όμ΄μ Έ μκ³ , κ° νμκ° κ²ΉμΉμ§ μκ² νλ©΄μ νμμ€μ μ¬μ©ν μ μλ νμμ μ΅λ κ°μλ₯Ό μ°Ύμ보μ. λ¨, νμλ νλ² μμνλ©΄ μ€κ°μ μ€λ¨λ μ μμΌλ©° ν νμκ° λλλ κ²κ³Ό λμμ λ€μ νμκ° μμλ μ μλ€. νμμ μμμκ°κ³Ό λλλ μκ°μ΄ κ°μ μλ μλ€. μ΄ κ²½μ°μλ μμνμλ§μ λλλ κ²μΌλ‘ μκ°νλ©΄ λλ€.
π₯μ λ ₯ 쑰건
11
1 4
3 5
0 6
5 7
3 8
5 9
6 10
8 11
8 12
2 13
12 14
π€μΆλ ₯ 쑰건
4
μ€μ νμ΄ μκ° : 10λΆ
μ λ ₯
κ³μ°
μΆλ ₯
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
Integer[][] time = new Integer[N][2];
for(int i=0; i<N; i++){
time[i][0] = sc.nextInt();
time[i][1] = sc.nextInt();
}
Arrays.sort(time, Collections.reverseOrder());
int count = 0;
int lastEndTime = 0;
for(int i=0; i<N; i++){
if(lastEndTime <= time[i][0]){
lastEndTime = time[i][1];
count++;
}
}
System.out.println(count);
}
}
Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Integer; cannot be cast to class java.lang.Comparable ([Ljava.lang.Integer; and java.lang.Comparable are in module java.base of loader 'bootstrap')
μ€λ₯ λ°μ//before
Arrays.sort(time, Collections.reverseOrder());
//after
Arrays.sort(time, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2){
if (o1[1] == o2[1]) {
return o1[0] - o2[0];
}
return o1[1] - o2[1];
}
});
μ€μ νμ΄ μκ° : 20λΆ(첫 νμ΄ μκ° ν¬ν¨)
μ λ ₯
κ³μ°
μΆλ ₯
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, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2){
if (o1[1] == o2[1]) {
return o1[0] - o2[0];
}
return o1[1] - o2[1];
}
});
int count = 0;
int lastEndTime = 0;
for(int i=0; i<N; i++){
if(lastEndTime <= time[i][0]){
lastEndTime = time[i][1];
count++;
}
}
System.out.println(count);
}
}