LeetCode 最长递增子序列的O(nlogn)详解

来源:互联网 发布:python核心编程第二版 编辑:程序博客网 时间:2024/05/17 06:17

/*****************************************************************************
*
* 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 yo
* 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?
*
* Credits:
* Special thanks to @pbrother for adding this problem and creating all test cases.
*
*****************************************************************************/
求最长递增子序列,此题中没有要求这个序列,因此:
只需要将将比前一个数大的压入数组,如果一个数比其数组中最大的数小,则在数组中二分查找找到它该在的位置。

//如果只要求最长递增子序列的长度int lengthOfLIS(vector<int>& nums) {    vector<int> v;    for (int i = 0; i<nums.size(); i++) {        if (v.size() == 0 || v.back()<nums[i])            v.push_back(nums[i]);        else {            int low = 0, high = v.size() - 1;            while (low <= high) {                int mid = low + (high - low) / 2;                if (v[mid]<nums[i]) low = mid + 1;                else  high = mid - 1;            }            v[low] = nums[i];        }    }    return v.size();}

下面是求这个最长递增子序列的序列,其中dp[i]为以i位置结尾的最长递增子序列的个数。

#include <iostream>#include <vector>#include<string>#include<algorithm>using namespace std;//求DPvector<int> getLIS(vector<int> &num){    vector<int> ivec; //help    int length = num.size();    vector<int> dp(length);    dp[0] = 1;    ivec.push_back(num[0]);    for (int i = 1; i < length; ++i) {        if (ivec.back() < num[i]) {            ivec.push_back(num[i]);            dp[i] = dp[i - 1] + 1;        }        else {            int low = 0, high = ivec.size() - 1;            while (low <= high) {                int mid = (low + high) / 2;                if (ivec[mid] < num[i])                     low = mid + 1;                else                     high = mid - 1;            }            ivec[low] = num[i];            dp[i] = low + 1;        }    }    return dp;}//求最长递归子序列vector<int> subArray(vector<int> &nums,vector<int> &dp) {    int len = 0, index = 0;    for (int i = 0; i < dp.size(); i++) {        if (dp[i] > len) {   //找到最长递增子序列的最后一个值            len = dp[i];            index = i;        }    }    vector<int> result(len);    result[--len] = nums[index];    for (int i = index; i >= 0; i--) {        if (nums[i] < nums[index] && dp[i] == dp[index] - 1) {            result[--len] = nums[i];            index = i;        }    }    return result;}int main() {    vector<int> n = { 3,5,6,2,5,4,19,5,6,7,12 };    vector<int>dp = getLIS(n);    vector<int>result = subArray(n, dp);    for (auto c : result)        cout << c << endl;}
0 0
原创粉丝点击