Algorithm


Problem Name: 646. Maximum Length of Pair Chain

You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti.

A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion.

Return the length longest chain which can be formed.

You do not need to use up all the given intervals. You can select pairs in any order.

 

Example 1:

Input: pairs = [[1,2],[2,3],[3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4].

Example 2:

Input: pairs = [[1,2],[7,8],[4,5]]
Output: 3
Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].

 

Constraints:

  • n == pairs.length
  • 1 <= n <= 1000
  • -1000 <= lefti < righti <= 1000

Code Examples

#1 Code Example with C Programming

Code - C Programming


int cmp(const void *a, const void *b) {
    int x = (*(int **)a)[0], y = (*(int **)b)[0];
    //printf("cmp: %d:%d\n", x, y);
    
    if (x  <  y) return -1;
    else if (x > y) return 1;
    return 0;
}
int findLongestChain(int** pairs, int pairsRowSize, int pairsColSize) {
    int i, j, max, *dp;
    
    dp = malloc(pairsRowSize * sizeof(int));
    //assert(dp);
    
    qsort(pairs, pairsRowSize, sizeof(int *), cmp);
    
    max = 1;
    for (i = 0; i  <  pairsRowSize; i ++) {
        dp[i] = 1;
        for (j = 0; j  <  i; j ++) {
            if (pairs[i][0] > pairs[j][1] &&
                dp[i] < dp[j] + 1) {
                dp[i] = dp[j] + 1;
                if (max  <  dp[i]) max = dp[i];
            }
        }
    }
    
    free(dp);
    
    return max;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
pairs = [[1,2],[2,3],[3,4]]

Output

x
+
cmd
2

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
    public int findLongestChain(int[][] pairs) {
        Arrays.sort(pairs, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[0];
            }
        });

        int[] temp = new int[pairs.length];
        Arrays.fill(temp, 1);
        for (int i=1; i < pairs.length; i++) {
            for (int j=0; j < i; j++) {
                if (pairs[j][1] < pairs[i][0]) {
                    temp[i] = Math.max(temp[i], temp[j] + 1);
                }
            }
        }

        return Arrays.stream(temp).max().getAsInt();
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
pairs = [[1,2],[2,3],[3,4]]

Output

x
+
cmd
2

#3 Code Example with Javascript Programming

Code - Javascript Programming


const findLongestChain = function(pairs) {
  pairs.sort((a, b) => a[1] - b[1])
  let end = pairs[0][1], res = 1
  for(let i = 1, len = pairs.length; i  <  len; i++) {
    if(pairs[i][0] > end) {
      res++
      end = pairs[i][1]
    }    
  }
  return res
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
pairs = [[1,2],[7,8],[4,5]]

Output

x
+
cmd
3

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def findLongestChain(self, pairs):
        pairs.sort(key = lambda x: x[1])
        res, pre = 1, pairs[0][1]
        for c, d in pairs[1:]:
            if pre < c:
                pre = d
                res += 1
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
pairs = [[1,2],[7,8],[4,5]]

Output

x
+
cmd
3
Advertisements

Demonstration


Previous
#645 Leetcode Set Mismatch Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#647 Leetcode Palindromic Substrings Solution in C, C++, Java, JavaScript, Python, C# Leetcode