Longest Increasing Subsequence

  • tags: [DP_Sequence]

Question

Problem Statement

Given a sequence of integers, find the longest increasing subsequence (LIS).

You code should return the length of the LIS.

Example

For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3

For [4, 2, 4, 5, 3, 7], the LIS is [4, 4, 5, 7], return 4

Challenge

Time complexity O(n^2) or O(nlogn)

Clarification

What's the definition of longest increasing subsequence?

  • The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
  • https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

题解

由题意知这种题应该是单序列动态规划题,结合四要素,可定义f[i]为前i个数字中的 LIC 数目,那么问题来了,接下来的状态转移方程如何写?似乎写不出来... 再仔细看看 LIS 的定义,状态转移的关键一环应该为数字本身而不是最后返回的结果(数目),那么理所当然的,我们应定义f[i]为前i个数字中以第i个数字结尾的 LIS 长度,相应的状态转移方程为f[i] = {1 + max{f[j]} where j < i, nums[j] < nums[i]}, 该转移方程的含义为在所有满足以上条件的 j 中将最大的f[j] 赋予f[i], 如果上式不满足,则f[i] = 1. 具体实现时不能直接使用f[i] = 1 + max(f[j]), 应为若if f[i] < 1 + f[j], f[i] = 1 + f[j]. 最后返回 max(f[]).

Python

class Solution:
    """
    @param nums: The integer array
    @return: The length of LIS (longest increasing subsequence)
    """
    def longestIncreasingSubsequence(self, nums):
        if not nums:
            return 0

        lis = [1] * len(nums)
        for i in xrange(1, len(nums)):
            for j in xrange(i):
                if nums[j] <= nums[i] and lis[i] < 1 + lis[j]:
                    lis[i] = 1 + lis[j]
        return max(lis)
sol=Solution()
nums=[4, 2, 4, 5, 3, 7]
sol.longestIncreasingSubsequence(nums)
class Solution:
    """
    @param nums: The integer array
    @return: The length of LIS (longest increasing subsequence)
    """
    def longestIncreasingSubsequence(self, nums):
        # write your code here
        if nums is None or not nums:
            return 0
        dp = [1] * len(nums)
        for curr, val in enumerate(nums):
            for prev in xrange(curr):
                if nums[prev] <= val:
                    dp[curr] = max(dp[curr], dp[prev] + 1)
        return max(dp)

源码分析

  1. 初始化数组,初始值为1
  2. 根据状态转移方程递推求得lis[i]
  3. 遍历lis 数组求得最大值

复杂度分析

使用了与 nums 等长的空间,空间复杂度 $$O(n)$$. 两重 for 循环,最坏情况下 $$O(n^2)$$, 遍历求得最大值,时间复杂度为 $$O(n)$$, 故总的时间复杂度为 $$O(n^2)$$.

Follow up

上述问题均只输出最大值,现在需要输出 LIS 中的每一个原始元素值。

题解1 - LIS

由于以上递归推导式只能返回最大值,如果现在需要返回 LIS 中的每个元素,直观来讲,构成 LIS 数组中的值对应的原数组值即为我们想要的结果。我们不妨从后往前考虑,依次移除 lis[i] 数组中的值(减一)和索引,遇到和 lis[i]的值相等的 LIS 时即加入到最终返回结果。

results matching ""

    No results matching ""