java 力扣 646.最长数对链

1.题目

java 力扣 646.最长数对链_第1张图片

2.解法

①一维数组+排序、动态规划

class Solution {
    public int findLongestChain(int[][] pairs) {
        int n = pairs.length;
        int[] dp = new int[n];
        Arrays.fill(dp, 1);
        Arrays.sort(pairs, (a, b) -> (a[0] - b[0]));

        for(int i = 1; i < n; i++){
            for(int j = 0; j < i; j++){
                if(pairs[i][0] > pairs[j][1]) dp[i] = Math.max(dp[i], dp[j] + 1);
            }
        }
        return dp[n - 1];
    }
}

 

你可能感兴趣的:(力扣,动态规划,java,leetcode,算法,数据结构)