Longest Increasing Subsequence

Dynamic Programming, Binary Search

Medium

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 than one LIS combination, it is only necessary for you to return the length.

  • Your algorithm should run in O(n^2) complexity.

Follow up: Could you improve it to O(nlogn) time complexity?

Analysis

https://leetcode.com/problems/longest-increasing-subsequence/solution/

Naive Approach & Intuition

Brute-Force (TLE) - O(2^n) time

recursively search: include or not include the next position, and find the maximum of them

Recursive with Memoization (MLE)

same as above, but stores intermediate results, thus reduce the repetitive search; however, will exceed memory limit

Dynamic Programming - O(n^2) time, O(n) space

https://leetcode.com/problems/longest-increasing-subsequence/solution/

State:

dp[i] - represents the length of the longest increasing subsequence possible considering the array elements up to the i-th index only, by necessarily including the i-th element.

即以i-th元素结尾,并且包含i-th元素的最长上升子序列的长度

State Transfer Function:

dp[i] = max(dp[j]) + 1,∀ 0 ≤ j < i

To calculate dp[i], we need to append current element (nums[i] ) for all possible nums[i] > nums[j] where j is one of

0, 1, 2, ..., i - 1

Initialization:

dp[0] = 1;

max = 1;

Since single element is considered as LIS of length 1

Final Answer:

LIS_length = max(dp[i]), ∀ 0 ≤ i < n

即循环遍历dp[i]以找到其中最大值

Dynamic Programming with Binary Search - O(nlogn) time, O(n) space

From Source: https://leetcode.com/problems/longest-increasing-subsequence/discuss/74824/JavaPython-Binary-search-O(nlogn)-time-with-explanation

@dietpepsi

tails[i] - tails is an array storing the smallest tail of all increasing subsequences with length i+1 in tails[i].

For example, say we have nums = [4,5,6,3], then all the available increasing subsequences are:

Three scenarios on updating the ending (tail) numbers array:

From Source: https://yanjia.me/zh/2018/11/05/70/

按照以下规则更新这些序列 – 如果nums[i]比所有序列的末尾都大,或等于最大末尾,说明有一个新的不同长度序列产生,我们把最长的序列复制一个,并加上这个nums[i]。 – 如果nums[i]比所有序列的末尾都小,说明长度为1的序列可以更新了,更新为这个更小的末尾。 – 如果在中间,则更新那个末尾数字刚刚大于等于自己的那个序列,说明那个长度的序列可以更新了。

Solution

Dynamic Programming - O(n^2) time, O(n) space

DP - O(n^2) time, O(n) space -- Easier to understand version

14 ms, faster than 49.36%

Dynamic Programming with Binary Search - O(nlogn) time, O(n) space

0ms, faster than 100%

DP + Binary Search With Comment

Reference

LeetCode discussion: https://leetcode.com/problems/longest-increasing-subsequence/discuss/74824/JavaPython-Binary-search-O(nlogn)-time-with-explanation

LeetCode Official Solution: https://leetcode.com/problems/longest-increasing-subsequence/solution/

GeeksforGeeks - N log N time: https://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/

Yanjia: https://yanjia.me/zh/2018/11/05/70/

Grandy Yang: http://www.cnblogs.com/grandyang/p/4938187.html

Last updated

Was this helpful?