LeetCode-300: Longest Increasing Subsequence

topic:

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

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.

Problem analysis:

Find the length of the longest increasing subsequence of an array.

Link:

Thought tags:

Algorithms: dynamic programming , binary search

answer:

1. Find the length of the longest increasing subarray, time complexity: O(nlogn)

  • Use a binary search method to find the position of the first number in the incrementing subarray that is greater than or equal to the current value;
  • If found, replace it with the current value; otherwise, add the current value to the incrementing subarray, indicating that the value is larger than the value of the subarray, and may be input into the subarray.
  • Example: nums = [5,6,7,1,2,8,3,4,0,5,9]:
    • Traverse to 7: res = [5,6,7];
    • Traverse to 2: res = [1,2,7];
    • Traverse to 8: res = [1,2,7,8];
    • Traverse to 3: res = [1,2,3,8];
    • Traverse to 4: res = [1,2,3,4];
    • Three elements left: res = [0,2,3,4,5,9];
    • Finally, we can get the length of the longest increasing subsequence, but it should be noted here that the obtained subsequence is not a real subsequence, but its length is correct.
  • The algorithm cannot get the longest increasing subsequence, only the length is calculated.
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. The method that can get the longest increasing subsequence, time complexity: O(n^2)

  • Requires an array that holds the length of the longest subsequence to the current element, and a predecessor array that holds the current element;
  • For each element, it needs to be compared with all elements before it to continuously update the length and predecessor of the longest subsequence;
  • LIS returns the length of the longest subsequence, and then goes through the predecessor array to find the first longest subsequence.
#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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324749399&siteId=291194637