LeetCode-300:Longest Increasing Subsequence (最长递增子序列)

题目:

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

例子

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.

问题解析:

求数组的最长递增子序列的长度。

链接:

思路标签:

算法:动态规划二分查找

解答:

1. 求最长递增子数组的长度,时间复杂度:O(nlogn)

  • 使用一个二分查找的方式,找到递增子数组中大于等于当前值的第一个数的位置;
  • 如果找到,则利用当前值替换;否则将当前值加入到递增子数组中,表明该值比子数组的值都大,可能输入子数组。
  • 例子:nums = [5,6,7,1,2,8,3,4,0,5,9]:
    • 遍历到 7: res = [5,6,7];
    • 遍历到 2: res = [1,2,7];
    • 遍历到 8: res = [1,2,7,8];
    • 遍历到 3: res = [1,2,3,8];
    • 遍历到 4: res = [1,2,3,4];
    • 剩下三个元素 : res = [0,2,3,4,5,9];
    • 最后我们就可以得到最长递增子序列的长度,但是这里要注意得到的子序列不是真正的子序列,然而其长度是正确的。
  • 该算法无法得到最长递增子序列,仅计算长度。
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        if(nums.size() <= 0) return 0;
        vector<int> res;
        for(int i=0; i<nums.size(); ++i){
            auto it = lower_bound(res.begin(), res.end(), nums[i]);
            if(it == res.end())
                res.push_back(nums[i]);
            else
                *it = nums[i];
        }

        return res.size();
    }
};

2. 可以得到最长递增子序列的方法,时间复杂度:O(n^2)

  • 需要一个保存到当前元素最长的子序列的长度的数组,以及一个保存当前元素的前驱数组;
  • 对于每个元素需要与其前面的所有元素进行比较,以不断更新最长子序列的长度和前驱;
  • LIS返回最长子序列的长度,然后通过前驱数组,找到第一个最长子序列。
#include <iostream>
#include <vector>
using namespace std;

/*
arr: int数组
pre: array同等长度的int数组 记录第i个结点的前驱
nIndex: 最大长度所在的下标
*/

int LIS(const vector<int> &arr, vector<int> &pre, int &nIndex) {
    int length = arr.size();
    if (length <= 1)
        return arr.size();

    //初始化当前最长递增子序列的最大长度数组 & 前驱数组
    vector<int> longest(length, 1); 
    for (int i = 0; i < length; i++) {
        pre.push_back(-1);
    }

    //记录最长的长度  初始化为1
    int nLis = 1;
    for (int i = 1; i < length; i++) {
        for (int j = 0; j < i; j++) {
            if (arr[j] <= arr[i]) {
                //如果递增 并且通过第j个结点可以达到更长则更新并记录前驱
                if (longest[i] < longest[j] + 1) {
                    longest[i] = longest[j] + 1;
                    pre[i] = j;
                }
            }
        }

        //统计最大的值及位置
        if (nLis < longest[i]) { 
            nLis = longest[i];
            nIndex = i;
        }
    }
    return nLis;
}

//获取最大长度的序列  主要通过前驱查找
void GetLis(const vector<int> &arr, const vector<int> &pre, vector<int> &lis, int nIndex) {
    while (nIndex >= 0)
    {
        lis.push_back(arr[nIndex]);
        nIndex = pre[nIndex];
    }
    //数组翻转
    reverse(lis.begin(), lis.end());
}

//输出序列
void Print(const vector<int> &arr) {
    for (int i = 0; i < arr.size(); i++)
        cout << arr[i] << " ";
    cout << endl;
}

int main()
{
    vector<int> arr = { 23,56,43,12,78,4,9,10,68,42 };
    vector<int> pre;
    int nIndex;
    int max = LIS(arr, pre, nIndex);
    vector<int> lis;
    GetLis(arr, pre, lis, nIndex);
    Print(arr);
    cout << "最大长度: " << max << endl;
    Print(lis);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Koala_Tree/article/details/80062211