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(最长增长子序列)的更多相关文章

  1. leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence

    Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...

  2. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  3. LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...

  4. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. 673. Number of Longest Increasing Subsequence最长递增子序列的数量

    [抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...

  6. [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  7. [leetcode]300. Longest Increasing Subsequence最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...

  8. 300 Longest Increasing Subsequence 最长上升子序列

    给出一个无序的整形数组,找到最长上升子序列的长度.例如,给出 [10, 9, 2, 5, 3, 7, 101, 18],最长的上升子序列是 [2, 3, 7, 101],因此它的长度是4.因为可能会有 ...

  9. Leetcode300. Longest Increasing Subsequence最长上升子序列

    给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...

  10. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

随机推荐

  1. spring data jpa和spring data redis同时配置时,出现Multiple Spring Data modules found, entering strict repository configuration mode错误

    问题说明 data jpa和data redis同时配置时,出现Spring modules spring Spring Data Release Train <dependencyManage ...

  2. python函数传参和返回值注意事项

    函数传参 空参数 定义函数时括号里面没有形参,调用时不用传参. def func(): print('null para.') # 调用 func() 位置传参 规定形参的数量,调用时必须传递相同数量 ...

  3. linux--基础知识3

    #linux虚拟终端切换 ctrl+alt+F(1-6) 总共支持六个终端切换 #mkdir /root/boot/目录  在指定路径下新建目录 #touch  /目录/文件名  在指定目录创建文件 ...

  4. 基于impi zabbix监控r720 测试过程

    1.F2进入服务器bios 修改network  使这台服务器能够被远程访问. 2.在远程的centos 7 服务器上安装  impitool工具包 #ipmitool -I lanplus -H X ...

  5. bzoj4773 负环 倍增+矩阵

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4773 题解 最小的负环的长度,等价于最小的 \(len\) 使得存在一条从点 \(i\) 到自 ...

  6. 前端每日实战:32# 视频演示如何用纯 CSS 创作六边形按钮特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xjoOeM 可交互视频教程 此视频 ...

  7. 美国知名Cloudflare网络公司遭中国顶尖黑客攻击

    最近中美贸易战愈演愈烈,美国知名Cloudflare网络公司的客户的分布式拒绝服务攻击今天在恶意流量方面达到了新的高度,黑客并袭击了该公司在欧洲和美国的数据中心.根据Cloudflare首席执行官马修 ...

  8. 对async 函数的研究

    async 函数 1.ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是什么?一句话,它就是 Generator 函数的语法糖. 前文有一个 Generator ...

  9. BZOJ 4407: 于神之怒加强版 莫比乌斯反演 + 线筛积性函数

    Description 给下N,M,K.求     Input 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行,每行为两个正整数N,M,其意 ...

  10. postman 简单使用教程

    Postman 安装   Postman 接口测试(Collection)   Postman 接口测试(测试用例)Postman 接口测试(变量与参数化)Postman 接口测试(非 UI 运行模式 ...