您的位置:首页 > 产品设计 > UI/UE

*Longest Increasing Subsequence

2016-01-15 01:28 489 查看
Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,
Given
[10, 9, 2, 5, 3, 7, 101, 18]
,
The longest increasing subsequence is
[2, 3, 7, 101]
, therefore the length is
4
. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n2) complexity.

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

解法一:O(Nlog(N))

讲解在此:
http://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/
Our strategy determined by the following conditions,

1. If A[i] is smallest among all end candidates of active lists, we will start new active list of length 1.

2. If A[i] is largest among all end candidates of active lists, we will clone the largest active list, and extend it by A[i].

3. If A[i] is in between, we will find a list with largest end element that is smaller than A[i]. Clone and extend this list by A[i]. We will discard all other lists of same length as that of this modified list.

Note that at any instance during our construction of active lists, the following condition is maintained.

“end element of smaller list is smaller than end elements of larger lists”.

It will be clear with an example, let us take example from wiki {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}.

其中tailtable是用来存储过程中出现的可能的每个list的最后一个element。其中第三种情况的时候,利用binarysearch在taitalbe中寻找到可以“放”nums[i]的位置。这个“放”就是discard原来的,replace当前的nums[i].

public class Solution {
public int lengthOfLIS(int[] nums)
{
if(nums==null||nums.length==0) return 0;
int[] tailtable = new int[nums.length];
tailtable[0] = nums [0];
int len = 1;
for(int i=0;i<nums.length;i++)
{
if(nums[i]<tailtable[0]) tailtable[0]=nums[i];
else if(nums[i]>tailtable[len-1]) tailtable[len++] = nums[i];
else
{
int pos = binarysearch(tailtable,-1,len-1,nums[i]);
tailtable[pos] = nums[i];
}
}
return len;
}

public int binarysearch(int[] tailtable, int left, int right, int key)
{
while(left<right-1)
{
int mid = left + (right-left)/2;
if(tailtable[mid]>=key)
{
right = mid;
}
else left = mid;
}
return right;
}
}


解法二:DP, O(n*n)
https://www.youtube.com/watch?v=CE2b_-XfVDk
public int lengthOfLIS(int[] nums)
{
// Base case
if(nums.length <= 1)
return nums.length;

// This will be our array to track longest sequence length
int T[] = new int[nums.length];

// Fill each position with value 1 in the array
for(int i=0; i < nums.length; i++)
T[i] = 1;

// Mark one pointer at i. For each i, start from j=0.
for(int i=1; i < nums.length; i++)
{
for(int j=0; j < i; j++)
{
// It means next number contributes to increasing sequence.
if(nums[j] < nums[i])
{
// But increase the value only if it results in a larger value of the sequence than T[i]
// It is possible that T[i] already has larger value from some previous j'th iteration
if(T[j] + 1 > T[i])
{
T[i] = T[j] + 1;
}
}
}
}

// Find the maximum length from the array that we just generated
int longest = 0;
for(int i=0; i < T.length; i++)
longest = Math.max(longest, T[i]);

return longest;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: