// This problem is a deformation problem of LIS, which needs to sort one dimension before using LIS // Optimization solution is to order one dimension in ascending order, and then descending order one dimension. Class Solution {public int maxEnvelopes(int[][] envelopes) {public int maxEnvelopes(int[][] envelopes) {if (envelopes.length == 0 || envelopes[0].length == 0) {
            return0; Array. sort(envelopes, new Comparator<int[]>() {@override public int compare(int[] o1, int[] o2) {if (o1[0] == o2[0]) {
                   return o1[1] - o2[1];
               } else {
                   returno1[0] - o2[0]; }}}); int[] dp = new int[envelopes.length]; int ans = 0; //LIS dynamic programming templatefor (int i = 0; i < envelopes.length; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if (envelopes[i][0] > envelopes[j][0] && envelopes[i][1] > envelopes[j][1]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            ans = Math.max(ans, dp[i]);

        }
        returnans; }}Copy the code