*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
The longest increasing subsequence is
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].
解法二:DP, O(n*n)
https://www.youtube.com/watch?v=CE2b_-XfVDk
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; }
相关文章推荐
- 《论vue在前后端分离项目中的实践之年终总结》
- iOS 蓝牙开发(四)BabyBluetooth蓝牙库介绍(转)
- js获取request参数
- js获取request的值
- EasyUI DataGrid根据字段动态合并单元格
- html之marquee详解
- (一) request
- ios UIScrollView
- UILabel的自适应宽高
- easyui换主题,并记录在cookie
- 常见移动设备的 CSS3 Media Query 整理(iPhone/iPad/Galaxy/HTC
- 用代码判断Android手机的Rom是MIUI或者Flyme,以及获取MIUI或者Flyme的版本
- UITableView优化技巧
- Arduino初初教程9——红外遥控
- UIScrollView监听静止的数种情况
- Bug死磕之hue集成的oozie+pig出现资源任务死锁问题
- UILabel 多行高度自适应 计算高度存在的 坑
- UI开发中Button基本方法
- UITableView中的cell的优化
- UITableView性能优化-一次面试后的反思总结