LC #354. 俄罗斯套娃信封问题

力扣的题有空再探索复杂度更低的做法

class Solution {
public:
    static bool cmp(vector<int> a,vector<int> b){
        if(a[0]!=b[0])return a[0]<b[0];
        else return a[1]<b[1];
    }
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        if(envelopes.size()<2)return envelopes.size();
        sort(envelopes.begin(),envelopes.end(),cmp);
        int len=envelopes.size();
        int num[len];
        fill(num,num+len,0);
        num[0]=1;
        int maxnum=1;
        for(int i=1;i<len;i++){
            int maval=0,maidx=-1;
            for(int j=0;j<i;j++){
                if(num[j]>maval && envelopes[j][0]<envelopes[i][0] && envelopes[j][1]<envelopes[i][1]){
                    maidx=j;
                    maval=num[j];
                }
            }
            if(maidx==-1)num[i]=1;
            else num[i]=maval+1;
            maxnum=max(maxnum,num[i]);
        }
        return maxnum;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值