LeetCode No354.俄罗斯套娃问题(最长上升子序列的升级版)

LeetCode No354.俄罗斯套娃问题(最长上升子序列的升级版)_第1张图片
思路:首先对envelops按照envelops[][0]排序,然后看envelops[][1]即可,跟最长上升子序列的题一样

class Solution {
    public int maxEnvelopes(int[][] envelopes) {
        if(envelopes.length <= 1){
            return envelopes.length;
        }
        Arrays.sort(envelopes, Comparator.comparingInt((o)->(o[0])));
        int[] dp = new int[envelopes.length];
        dp[0] = 1;
        int res = 1;
        for (int i = 1; i < envelopes.length; i++) {
            dp[i] = 1;
            for (int j = i-1; j >= 0; j--) {
                if(envelopes[j][1] < envelopes[i][1] && envelopes[j][0] < envelopes[i][0]){
                    dp[i] = Math.max(dp[i],dp[j] + 1);
                }
            }
            res = Math.max(res,dp[i]);
        }
        return res;
    }
}

LeetCode No354.俄罗斯套娃问题(最长上升子序列的升级版)_第2张图片

你可能感兴趣的:(#,leetcode,leetcode,动态规划,算法)