package leetcode;

/**
 * https://leetcode.com/problems/maximum-length-of-pair-chain/
 */
public class Problem646 {
    public int findLongestChain(int[][] pairs) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem646 prob = new Problem646();
        System.out.println(prob.findLongestChain(new int[][]{{1, 2}, {2, 3}, {3, 4}})); // 2
    }
}

@@ -12,16 +12,42 @@
 tcode;%0A%0A
+import java.util.Arrays;%0A%0A
 /**%0A * h
@@ -196,32 +196,751 @@
     
-// TODO%0A        return 0
+Arrays.sort(pairs, (a, b) -%3E %7B%0A            int cmp = Integer.compare(a%5B0%5D, b%5B0%5D);%0A            if (cmp == 0) %7B%0A                return Integer.compare(a%5B1%5D, b%5B1%5D);%0A            %7D%0A            return cmp;%0A        %7D);%0A        return findLongestChain(pairs, 0);%0A    %7D%0A%0A    private static int findLongestChain(int%5B%5D%5B%5D pairs, int idx) %7B%0A        if (idx == pairs.length) %7B%0A            return 0;%0A        %7D%0A        int max = 0;%0A        int%5B%5D current = pairs%5Bidx%5D;%0A        for (int i = idx + 1; i %3C pairs.length; i++) %7B%0A            int%5B%5D next = pairs%5Bi%5D;%0A            int val = 1;%0A            if (current%5B1%5D %3C next%5B0%5D) %7B%0A                val++;%0A            %7D%0A            max = Math.max(max, findLongestChain(pairs, i + 1) + val);%0A        %7D%0A        return max
 ;%0A  
@@ -1033,16 +1033,18 @@
 m646();%0A
+//
         
@@ -1128,16 +1128,475 @@
 ); // 2%0A
+//        System.out.println(prob.findLongestChain(new int%5B%5D%5B%5D%7B%7B1, 2%7D, %7B2, 3%7D, %7B3, 4%7D, %7B4, 5%7D, %7B5, 6%7D%7D)); // 3%0A//        System.out.println(prob.findLongestChain(new int%5B%5D%5B%5D%7B%7B1, 2%7D, %7B2, 3%7D, %7B3, 4%7D, %7B4, 5%7D%7D)); // 2%0A        System.out.println(prob.findLongestChain(new int%5B%5D%5B%5D%7B%7B1, 2%7D, %7B2, 3%7D, %7B3, 4%7D, %7B4, 5%7D, %7B5, 6%7D, %7B7, 8%7D%7D)); // 3%0A//        System.out.println(prob.findLongestChain(new int%5B%5D%5B%5D%7B%7B-1, 0%7D, %7B1, 2%7D, %7B2, 3%7D, %7B3, 4%7D, %7B4, 5%7D, %7B5, 6%7D, %7B7, 8%7D%7D)); // 5%0A
     %7D%0A%7D%0A

Update problem 646