65.Longest Increasing Subsequence(最长增长子序列)
Level:
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(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?
思路分析:
思路一:动态规划的思想解决。dp[ i ]表示以第i个元素结尾的最长递增子序列的长度,那么状态转移方程就为 if(nums[ j ]<nums[ i ]) dp[ i ]=max(dp[ j ]+1,dp[ i ]);这个递推方程的意思是,在求以nums[ i ]为末尾元素的最长递增子序列时,找到所有序号在 i 之前的且小于nums[ i ]的元素nums[ j ],即 j < i,且 nums[ j ]<nums[ i ]。如果这样的元素存在,那么对于所有的nums[ j ]都有一个以nums[ i ]结尾的最长递增子序列 ,我们将其中的最长序列选出来就是,以nums[ i ]结尾的最长递增子序列长度,如果这样的元素不存在,那么我们以nums[ i ]自身组成一个以它为结尾的递增子序列。算法的时间复杂度为O(n)。
思路二:设计一个容器来存放最长上升子序列,先将nums[0]放入,遍历nums,如果访问到的元素大于,容器尾部的元素,直接加到尾部,如果小于,则使用二分查找找到第一个比其大的元素,将其替换。最终访问完成时,容器中就是最长上升子序列
假设要寻找最长上升子序列的序列是a[n],然后寻找到的递增子序列放入到数组b中。
(1)当遍历到数组a的第一个元素的时候,就将这个元素放入到b数组中,以后遍历到的元素都和已经放入到b数组中的元素进行比较;
(2)如果比b数组中的每个元素都大,则将该元素插入到b数组的最后一个元素,并且b数组的长度要加1;
(3)如果比b数组中最后一个元素小,就要运用二分法进行查找,查找出第一个比该元素大的最小的元素,然后将其替换。
在这个过程中,只重复执行这两步就可以了,最后b数组的长度就是最长的上升子序列长度。例如:如该数列为:
5 9 4 1 3 7 6 7
那么:
5 //加入
5 9 //加入
4 9 //用4代替了5
1 9 //用1代替4
1 3 //用3代替9
1 3 7 //加入
1 3 6 //用6代替7
1 3 6 7 //加入
最后b中元素的个数就是最长递增子序列的大小,即4。
要注意的是最后数组里的元素并不就一定是所求的序列,例如如果输入 2 5 1那么最后得到的数组应该是 1 5而实际上要求的序列是 2 5
代码:
思路一
public class Solution{
public int lengthOfLIS(int []nums){
if(nums==null||nums.length==0)
return 0;
int []dp=new int [nums.length];
dp[0]=1; //表示以nums[0]为结尾的最长递增子序列
int res=1;
for(int i=1;i<nums.length;i++){
dp[i]=1;
for(int j=0;j<i;j++){
if(nums[j]<nums[i]){
dp[i]=Math.max(dp[j]+1,dp[i]);
res=Math.max(res,dp[i]);
}
}
}
return res;
}
}
思路二
public class Solution{
public int lengthOfLIS(int []nums){
if(nums==null||nums.length==0)
return 0;
int []temp=new int [nums.length];
int size=0;
temp[size]=nums[0];
for(int i=1;i<nums.length;i++){
if(nums[i]>temp[size]){
size++;
temp[size]=nums[i];
}else{
int t=search(0,size,temp,nums[i]); //找到第一个大于nums[i]的数
temp[t]=nums[i];
}
}
return size+1;
}
public int search(int start,int end,int []temp,int k){//二分查找第一个大于k的值
while(start<=end){
int mid=(start+end)/2;
if(temp[mid]<k){
start=mid+1;
}else{
end=mid-1;
}
}
return start;
}
}
65.Longest Increasing Subsequence(最长增长子序列)的更多相关文章
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- [leetcode]300. Longest Increasing Subsequence最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
- 300 Longest Increasing Subsequence 最长上升子序列
给出一个无序的整形数组,找到最长上升子序列的长度.例如,给出 [10, 9, 2, 5, 3, 7, 101, 18],最长的上升子序列是 [2, 3, 7, 101],因此它的长度是4.因为可能会有 ...
- Leetcode300. Longest Increasing Subsequence最长上升子序列
给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
随机推荐
- 07-求解Ax=0:主变量、特解
一.定义转向算法 在第六节讲了空间,列空间,零空间的定义,这节主要讲解如何求出这些空间,即求解$Ax=0$的过程是怎么样的过程,以下面的矩阵$A$为例:(这里主要是长方阵) $A=\left[\beg ...
- Codeforces Round #425 (Div. 2) - A
题目链接:http://codeforces.com/contest/832/problem/A 题意:有n个棍子,两个人轮流取这些棍子,每个人每次只能去恰好k个棍子(不足k个则不能取),问先手取的棍 ...
- python列表转json树菜单
1.列表数据 data = [ { 'id': 1, 'parent_id': 2, 'name': "Node1" }, { 'id': 2, 'parent_id': 5, ' ...
- 碎片记录——JMeter之 http post json对象与参数化调用,以及beanshell 引用Java源码
参考文档 http://jmeter.apache.org/usermanual/component_reference.html#samplers https://blog.csdn.net/qq_ ...
- 方法返回前面有if - else if - else ,最终返回值是?
-(NSString *)testA{ int a = ?; ) { return @"a大于5"; } ) { return @"a不大于5"; } retu ...
- JS一些概念知识及参考链接
1.setTimeout.setInterval.promise.宏任务.微任务 先执行宏任务整体 script 同步代码,然后遇到 setTimeout 或者 setInterval 即放到宏任务队 ...
- 【HDOJ6635】Nonsense Time(时间倒流,lis)
题意:给定n个数的数列,第i个数为a[i],刚开始所有位置都处于禁用状态,第i次之后位置p[i]变为可用,求每次变化后的lis长度 n,a[i],p[i]<=5e4 保证a[i],p[i]均为随 ...
- [IOI2018] meetings 会议
https://www.luogu.org/problemnew/show/P5044 题解 这种关于最大值或者最小值的问题,可以往笛卡尔树的方面想. 先考虑一个朴素的\(dp\),设\(dp[l][ ...
- 攻防世界 | string
#encoding=utf-8 #!usr/bin/python from pwn import * io = remote('111.198.29.45',42643) io.recvuntil(& ...
- 《数据结构与算法(C语言版)》严蔚敏 | 第五章 建立二叉树,并完成三/四种遍历算法
PS:所有的代码示例使用的都是这个图 2019-10-29 利用p126的算法5.3建立二叉树,并完成三种遍历算法 中序 后序 先序 #include<iostream> #include ...