题目描述
贪心
贪心
import java.util.*;
public class Main {
static class Range {
int l, r;
public Range(int l, int r) {
this.l = l;
this.r = r;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Range[] range = new Range[n];
for (int i = 0; i < n; i++) {
int l = scanner.nextInt();
int r = scanner.nextInt();
range[i] = new Range(l, r);
}
//使用lambda表达式进行比较并排序
Arrays.sort(range, (a, b) -> a.r - b.r);
int res = 0;
int ed = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (range[i].l > ed) {
res++;
ed = range[i].r;
}
}
System.out.println(res);
}
}