[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 

Longest Bitonic Subsequence

[Coding Made Simple] Maximum Sum Increasing Subsequence

Largest Divisible Subset

Frog Jump

Russian Doll Envelopes 

转载于:https://www.cnblogs.com/lz87/p/7203674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值