【Leecode 354】俄罗斯套娃信封问题(排序+LIS)

题目:

题目链接

思路:排序+LIS

LIS最长上升子序列 算法模板

代码实现

#define ls envelopes
class Solution {
public:
    struct ss{
        int w,h;
        bool operator <(const ss b){
            if(w==b.w) return h>b.h;
            else return w<b.w;
        }
    }xf[100005];
    int find(int *a,int x,int l,int r){//二分查找
        while(l<r){
            int mid=(l+r)>>1;
            if(x< a[mid] ) r=mid;
            else if(x> a[mid] ) l=mid+1;
            else l=r=mid;
        }return l;
    }
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        for(int i=0;i<envelopes.size();i++)
            xf[i]=(ss){ls[i][0],ls[i][1]};
        sort(xf,xf+ls.size());
        //排序后问题转化为:求宽度、高度的最长 同时上升子序列(二维LIS)
/*****处理:对于同等宽度的信封,将高度较高的那个排到最前面,这样可以保证上升序列中,不会出现等宽的两个信封被同时计入****/
//这样处理之后,问题直接化简为:高度的最长上升子序列(模板LIS)
        int low[100005];//LIS算法
        int ans=0;low[0]=0;
        for(int i=0;i<ls.size();i++)
        if(xf[i].h>low[ans]){
            low[++ans]=xf[i].h;
        //    cout<<ans<<":"<<xf[i].w<<" "<<xf[i].h<<endl;
        }else if(xf[i].h<low[ans]){
            int id=find(low,xf[i].h,1,ans);
            low[id]=xf[i].h;
        }
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GoesM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值