LeetCode——354. Envelope for Matryoshka

Title description:

Given some envelopes marked with width and height, the width and height appear as integer pairs (w, h). When the width and height of another envelope are larger than this envelope, this envelope can be put into another envelope, just like a Russian doll.
Please calculate the maximum number of envelopes that can form a set of "Russian doll" envelopes (that is, you can put one envelope in another envelope).

Note:
Rotating envelopes is not allowed.

Example:
Input: envelopes = [[5,4],[6,4],[6,7],[2,3]]
Output: 3
Explanation: The maximum number of envelopes is 3, the combination is: [2, 3] => [5,4] => [6,7].

Idea: First arrange the width W in ascending order, and if the W is the same, arrange it in descending order according to the height H. After that, use all H as an array, and the length of LIS (longest increasing subsequence) calculated on this array is the answer.
Why do we arrange in descending order of height H when W is the same?
Because two envelopes with the same W cannot contain each other, when W is the same, arrange H in reverse order, then at most one of these reverse H will be selected as an increasing subsequence, ensuring that no identical W will appear in the final envelope sequence. Happening.

The C++ code is as follows:

class Solution {
    
    
public:
    int maxEnvelopes(vector<vector<int>>& envelopes) {
    
    
        if(envelopes.size()==0){
    
    
            return 0;
        }
        int h=envelopes.size();
        int l=envelopes[0].size();
        sort(envelopes.begin(), envelopes.end(), [](vector<int>a, vector<int>b) {
    
    return a[0]==b[0]?a[1] > b[1]:a[0]<b[0]; });//先对宽度W进行升序排列,如果遇到W相同的情况下,则按照高度H降序排列,这是重点。
        vector<int>v;
        for(int i=0;i<h;i++){
    
    
            v.push_back(envelopes[i][1]);
        }
        return IFS(v);
    }
    int IFS(vector<int>&v){
    
    
        int size=v.size();
        vector<int>dp(size,1);
        int ans=0;
        for(int i=0;i<size;i++){
    
    
            for(int j=0;j<i;j++){
    
    
                if(v[j]<v[i]){
    
    
                    dp[i]=max(dp[i],dp[j]+1);
                }
            }
            ans=max(ans,dp[i]);
        }
        return ans;
    }
};

The JAVA code is as follows:

class Solution {
    
    
    public int maxEnvelopes(int[][] envelopes) {
    
    
        int n = envelopes.length;
        Arrays.sort(envelopes, new Comparator<int[]>() {
    
    
            @Override
            public int compare(int[] o1, int[] o2) {
    
    
                return o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0];
            }
        });
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
    
    
            h[i] = envelopes[i][1];
        }
        return lengthOfLIS(h);
    }

    public int lengthOfLIS(int[] nums) {
    
    
        int n = nums.length;
        int[] dp = new int[n];
        Arrays.fill(dp, 1);
        int ans = 0;
        for (int i = 0; i < n; i++) {
    
    
            for (int j = 0; j < i; j++) {
    
    
                if (nums[i] > nums[j]) {
    
    
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            ans = Math.max(dp[i], ans);
        }
        return ans;
    }
}

Results of the:
Insert picture description here

Guess you like

Origin blog.csdn.net/FYPPPP/article/details/111666890