[leetcode]300. Longest Increasing Subsequence

本文介绍了求解最长递增子序列(LIS)问题的三种算法实现:一种超时的方法、动态规划方法及改进后的O(n log n)复杂度方法。通过具体示例展示了不同方法的特点,并给出了详细的代码实现。

题目链接:https://leetcode.com/problems/longest-increasing-subsequence/#/description

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

方法一(超时):解法参考

class Solution{
public:
    int lengthOfLIS(vector<int>& nums)
    {
        int maxLength=0;
        for(int i=1;i<=nums.size();i++)
        {
            vector<int> tmp(i,INT_MAX);
            for(int j=0;j<nums.size();j++)
            {
                for(int k=0;k<i;k++)
                {
                    if(nums[j]<=tmp[k])
                    {
                        tmp[k]=nums[j];
                        break;
                    }
                }
            }
            int i=0;
            while(tmp[i]!=INT_MAX && i<tmp.size())
            {
                i++;
            }
            if(i>maxLength)
                maxLength=i;
        }
        return maxLength;
    }
};


方法二:

动态规划:

class Solution{
public:
    int lengthOfLIS(vector<int>& nums)
    {
        int maxLength=1;
        if(nums.size()==0)
            return 0;
        vector<int> res(nums.size(),1);//vector[i]表示前i个数中具有的最大连续长度
 
        for(int i=0;i<nums.size();i++)
        {
            for(int j=0;j<i;j++)
            {
                if(nums[i]>nums[j])
                {
                    res[i]=max(res[i],res[j]+1);
                    if(res[i]>maxLength)
                        maxLength=res[i];
                }
            }
        }
        return maxLength;
    }
};
class Solution{
public:
    int lengthOfLIS(vector<int>& nums)
    {
        int maxLength=1;
        if(nums.size()==0)
            return 0;
        vector<int> res(nums.size(),1);//vector[i]表示前i个数中具有的最大连续长度

        for(int i=0;i<nums.size();i++)
        {
            for(int j=i+1;j<nums.size();j++)
            {
                if(nums[j]>nums[i])
                {
                    res[j]=max(res[j],res[i]+1);
                    if(res[j]>maxLength)
                        maxLength=res[j];
                }
            }
        }
        return maxLength;
    }
};


方法三:

思路:新建一个数组res,把nums[ 0 ]放进数组,随后遍历nums[ 1 ] - nums[ nums.size()-1 ],如果nums[ i ]比res.back()大,则把nums[ i ]放进res的后面,否则,把nums[ i ]替换res中第一个比nums[ i ]大的数,可参考

举例:原序列为1,5,8,3,6,7

开始1,5,8相继加入数组V,此时读到3,用3替换5,得到1,3,8;

 再读6,用6替换8,得到1,3,6;

再读7,得到最终数组为1,3,6,7。最长递增子序列为长度

public:
    int lengthOfLIS(vector<int>& nums)
    {
        if(nums.size()==0)
            return 0;
        vector<int> res;
        res.push_back(nums[0]);
        for(int i=1;i<nums.size();i++)
        {
            if(nums[i]>res.back())
                res.push_back(nums[i]);
            else
                *lower_bound(res.begin(),res.end(),nums[i])=nums[i];
        }
        return res.size();
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值