首页 > 代码库 > [LintCode] Longest Increasing Subsequence

[LintCode] Longest Increasing Subsequence

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

You code should return the length of the LIS.

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_increasing_subsequence

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 [2, 4, 5, 7], return 4

Challenge 

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

 

According to the definition of subsequence, we can‘t apply sorting as it changes the relative ordering of the orginal array. 

 

Solution 1. Dynamic Programming, O(n^2) runtime, O(n) space

Since the problem asks for an optimal result, the longest increasing subsequence, we should consider dynamic programming. 

A sequence must end at one element,  so we can break the original problem into n subproblems as calculating the LIS whose

ending element is nums[0], nums[1]......., nums[n - 1].

 

For each subproblem calculating the LIS whose ending element is nums[i], 

LIS(i) = max{LIS(j) + 1}, for j = 0 ....... i - 1 and nums[j] < nums[i]

 

With the above recursive formula, we can solve this problem recursively as follows.

 1 public class Solution {
 2     private int max = 0;
 3     public int longestIncreasingSubsequence(int[] nums) {
 4         for(int i = 0; i < nums.length; i++){
 5             helper(nums, i);
 6         }
 7         return max;    
 8     }
 9     private int helper(int[] nums, int endIdx){
10         int len = 1;
11         for(int j = 0; j < endIdx; j++){
12             if(nums[j] < nums[endIdx]){
13                 len = Math.max(len, helper(nums, j) + 1);
14             }
15         }    
16         if(len > max){
17             max = len;
18         }
19         return len;
20     }
21 }

 

The problem of this recursive solution is that we have overlapping subproblems.

For example, let‘s assume j < k < i, nums[j] < nums[k] < nums[i]. 

When calculating LIS(k) and LIS(i), LIS(j) are calculated twice, doing redudant work.

To get rid of this redundancy, we use dynamic programming.

State: len[i]: the LIS that ends at nums[i];

Function: len[i] = Max(len[i], len[j] + 1), for j = 0..... i - 1 && nums[j] < nums[i];

O(n) space can‘t be optimized further as for any given subproblems, all its smaller subproblems‘

results are needed to calculate its LIS.

 1 public class Solution {
 2     public int longestIncreasingSubsequence(int[] nums) {
 3         int[] len = new int[nums.length];
 4         int max = 0;
 5         for (int i = 0; i < nums.length; i++) {
 6             len[i] = 1;
 7             for (int j = 0; j < i; j++) {
 8                 if (nums[j] < nums[i]) {
 9                     len[i] = len[i] > len[j] + 1 ? len[i] : len[j] + 1;
10                 }
11             }
12             if (len[i] > max) {
13                 max = len[i];
14             }
15         }
16         return max;
17     }
18 }

 

 

Solution 2. O(n * log n) runtime using Binary search

 

 

Related Problems 

Largest Divisible Subset

Frog Jump

Russian Doll Envelopes 

[LintCode] Longest Increasing Subsequence