-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_646.java
More file actions
30 lines (28 loc) · 742 Bytes
/
a_646.java
File metadata and controls
30 lines (28 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package dynamicProgramming;
import java.util.Arrays;
import java.util.Comparator;
/**
* 一组整数对能够构成的最长链(贪心)
*/
public class a_646 {
public int findLongestChain(int[][] pairs) {
if (pairs == null && pairs.length == 0) {
return 0;
}
Arrays.sort(pairs, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1] - o2[1];
}
});
int res = 1;
int last = pairs[0][1];
for (int i = 1; i < pairs.length; i++) {
if (pairs[i][0] > last) {
res++;
last = pairs[i][1];
}
}
return res;
}
}