最长递增子序列(Longest increasing subsequence)

问题定义:

给定一个长度为N的数组A,找出一个最长的单调递增子序列(不要求连续)。

这道题共3种解法。

1. 动态规划

动态规划的核心是状态的定义和状态转移方程。定义lis(i),表示前i个数中以A[i]结尾的最长递增子序列的长度。可以得到以下的状态转移方程:

d(i) = max(1, d(j) + 1), 其中j < i,且A[j] <= A[i]

程序实现:

int longestIncreasingSubsequence(vector<int> nums)
{
    if (nums.empty())
        return 0;
    int len = nums.size();
    vector<int> lis(len, 1);    

    for (int i = 1; i < len; ++i)
    {
        for (int j = 0; j < i; ++j)
        {
            if (nums[j] < nums[i] && lis[i] < lis[j] + 1)
                lis[i] = lis[j] + 1;
        }
    }
    return *max_element(lis.begin(), lis.end());
}

程序复杂度为O(N^2)

2. 动态规划 + 二分查找

换一种角度看问题。令Ai,j表示所有长度为j的最大递增子序列的最小末尾,我们有Ai,1 < Ai,2 < ... < Ai,j。

对A[i+1]来说,有两种选择。

1. Ai,j < A[i+1], 此时我们可以得到一个长度为i+1的最大递增子序列。

2. 替换Ai,k,如果Ai,k-1 < A[i+1] < Ai,k。

替换长度为k的最大递增子序列的最小末尾,是为了增大获取更长子序列的机会。

程序实现:

int binarySearch(const int arr[], int low, int high, int val)
{
    while (low <= high)
    {
        int mid = low + (high - low) / 2;    // Do not use (low + high) / 2 which might encounter overflow issue

        if (val < arr[mid])
            high = mid - 1;
        else if (val > arr[mid])
            low = mid + 1;
        else
            return mid;
    }
    return low;
}
int LIS(int arr[], int n)
{
    int *minTail = new int[n];
    minTail[0] = arr[0];
    int len = 1;
    for (int i = 1; i < n; ++i)
    {
        if (arr[i] > minTail[len-1])
            minTail[len++] = arr[i];
        else
        {
            int pos = binarySearch(minTail, 0, len-1, arr[i]);
            minTail[pos] = arr[i];
        }
    }
    delete [] minTail;
    return len;
}

复杂度:O(nlogn)

reference :

最长递增子序列(LIS)

3. 排序+LCS

这种方法就不细说了。。。

时间: 2024-10-04 04:02:54

最长递增子序列(Longest increasing subsequence)的相关文章

算法实践--最长递增子序列(Longest Increasing Subsquence)

什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5}, {1} 其中最长的递增子序列是{2, 4, 5} 问题:对于长度为N的矢量D,如何找到它的最长递增子序列 一个简单的算法 for (i=N; i>0; --i) {1. 找到所有长度为i的子序列; //复杂度为(N!)/(i!)(N-i)! O(exp(N)) 2. 判断是否其中有一个为递增子

nlog(n)解动态规划--最长上升子序列(Longest increasing subsequence)

最长上升子序列LIS问题属于动态规划的初级问题,用纯动态规划的方法来求解的时间复杂度是O(n^2).但是如果加上二叉搜索的方法,那么时间复杂度可以降到nlog(n).  具体分析参考:http://blog.chinaunix.net/uid-26548237-id-3757779.html 代码: #include <iostream> using namespace std; int LIS_nlogn(int *arr, int len) { int *LIS = new int[len

最长上升子序列Longest Increasing Subsequence

给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是?[2,3,7,101],它的长度是 4. 方法1:时间复杂度n2,容易想到,记录数组中每个元素作为上升子序列最后一个元素的最大长度. import java.math.*; class Solution { public int lengthOfLIS(int[] nums) { int[] res = new int[nums.length

[Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed). E

LeetCode 673. Number of Longest Increasing Subsequence 最长递增子序列的个数 (C++/Java)

题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] O

[leetcode]300. Longest Increasing Subsequence最长递增子序列

Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be more

POJ 2533 Longest Ordered Subsequence【最长递增子序列】【DP思想】

Longest Ordered Subsequence Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 6   Accepted Submission(s) : 1 Problem Description A numeric sequence of ai is ordered ifa1 < a2 < ... < aN. Let t

LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Note: There may be

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu