-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_646.java
57 lines (54 loc) · 2.02 KB
/
_646.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.fishercoder.solutions.firstthousand;
import java.util.Arrays;
public class _646 {
/*
* Although this problem could be solved using DP, greedy is more efficient in both time and space complexity.
*/
public static class Solution1 {
/*
* credit: https://leetcode.com/problems/maximum-length-of-pair-chain/editorial/
*/
public int findLongestChain(int[][] pairs) {
// sort by the second element
Arrays.sort(pairs, (a, b) -> a[1] - b[1]);
int ans = 0;
int prev = Integer.MIN_VALUE;
for (int[] pair : pairs) {
if (pair[0] > prev) {
ans++;
prev = pair[1];
}
}
return ans;
}
}
public static class Solution2 {
/*
* credit: https://leetcode.com/problems/maximum-length-of-pair-chain/discuss/105623/Java-Very-Simple-without-DP
*/
public int findLongestChain(int[][] pairs) {
// sort by the first element
Arrays.sort(pairs, (a, b) -> a[0] - b[0]);
int len = 0;
int pre = Integer.MIN_VALUE;
for (int[] pair : pairs) {
if (pair[0] > pre) {
// no overlap
len++;
// so we need to update the previous number to be the end of current pair:
// pair[1]
pre = pair[1];
} else if (pair[1] < pre) {
// overlap but with a smaller second number
// since we want to find the maximum possible chain, so we update pre to be this
// smaller number
// this means we decided to adopt this pair to be in this chain and give up the
// previous one
// this logic can be seen clearly in test3 for this class
pre = pair[1];
}
}
return len;
}
}
}