Leetcode_面试题 17.08. 马戏团人塔(二维LIS)

求严格上升的二维LIS,注意要第一维升序,然后相同的按第二维降序,原理就是这样保证第一维相同的这些元素只有可能被选中一个加入LIS

code

class Solution {
public:
    struct node{
        int a,b;
        bool operator <(const node& rhs)const{
            if(a!=rhs.a){
                return a<rhs.a;
            }else{
                return b>rhs.b;
            }
        }
    };
    int bestSeqAtIndex(vector<int>& height, vector<int>& weight) {
        int n=height.size();
        vector<node> v;
        for(int i=0;i<n;i++){
            v.push_back(node{height[i],weight[i]});
        }
        sort(v.begin(),v.end());
        vector<int> g(n+5,0x3f3f3f3f);
        int ans=0;
        for(int i=0;i<n;i++){
            int k=lower_bound(g.begin(),g.end(),v[i].b)-g.begin();
            g[k]=v[i].b;
            ans=max(ans,k+1);
        }
        return ans;
    }
};

猜你喜欢

转载自www.cnblogs.com/zxcoder/p/12561098.html
今日推荐