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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章