Likou 354. The Russian Dolls Envelope Issue LIS sort

https://leetcode-cn.com/problems/russian-doll-envelopes/
Insert picture description here
Idea: First follow www Sort the input from small to large, then the problem is converted toLIS LISL I S , but there are still some points that need to be paid attention to. Thew, hw, h of theenvelopeThe requirements of w and h are monotonically increasing, so after sorting,wwEnvelopes with equal w can only choose1 11. How to guarantee this? InwwWhen w is equal, we can followhhh is sortedfrom largest to smallest, so thatLIS LIS iscalculatedL I S is the samewe weYou must choose at most one envelope for w e .

class Solution {
    
    
public:
    int maxEnvelopes(vector<vector<int>>& envelopes) {
    
    
        sort(envelopes.begin(),envelopes.end(),[](const vector<int>& v1,const vector<int>& v2){
    
    
            return v1[0]<v2[0]||(v1[0]==v2[0]&&v1[1]>v2[1]);
        });
        vector<int> ans;
        for(const vector<int>& ele:envelopes)
        {
    
    
            if(ans.empty()||ele[1]>ans.back())
                ans.push_back(ele[1]);
            else 
                *lower_bound(ans.begin(),ans.end(),ele[1])=ele[1];
        }
        return ans.size();
    }
};

Guess you like

Origin blog.csdn.net/xiji333/article/details/114371761