646 Maximum Length of Pair Chain
1. Question
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation:
The longest chain is [1,2] -> [3,4]2. Implementation
3. Time & Space Complexity
Last updated
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation:
The longest chain is [1,2] -> [3,4]Last updated
class Solution {
public int findLongestChain(int[][] pairs) {
Arrays.sort(pairs, (a, b)->(a[0] - b[0]));
int n = pairs.length;
int[] LIS = new int[n];
Arrays.fill(LIS, 1);
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (pairs[j][1] < pairs[i][0] && (LIS[j] + 1 > LIS[i])) {
LIS[i] = LIS[j] + 1;
}
}
}
int res = 1;
for (int len : LIS) {
res = Math.max(res, len);
}
return res;
}
}