LIS-最长递增子序列的长度-java
来源:互联网 发布:我的世界商店js 编辑:程序博客网 时间:2024/06/06 15:53
LIS序列不要求连续,查看资料发现一般有两种时间复杂度的算法,分别是O(n^2)和O(n*log n),下面分析的是复杂度为 O(n^2)的算法:
dp[i]表示以a[i]结尾的所有子序列中符合原则——递增不减的序列(因为符合条件的还是有很多序列的)中的最长长度,对于每一个a[i],有一个子序列只包括自己,故a[i]的最长递增子序列长度的最小值是1。
思路分析:
有数组arr[1…n],设最长递增子序列的长度为dp,发现dp与变量i相关,故需要分析变量dp[i]的意义
1. 若有a[i]>a[j] (j属于0...i-1)则有子序列:以a[i]结尾,还包括a[j],则这样的子序列的长度为以a[j]结尾的递增子序列的最大长度+1(因为前面提到a[i]的最长递增子序列长度的最小值是1),即dp[i]=dp[j]+1; 2. 对于j属于0...i-1中的某一个值,若a[i]<a[j],则dp[i]=1; 3. 遍历数组dp[i] (i属于0...arr.length-1), 找出最大值,该最大值就是该给定序列的最长递增子序列的长度
public class LIS { public static void main(String[] args) { // int[] arr = {9, -8,-1, 6, 7,-2, 0}; int[] arr = { 9, -8, -1, 6, 5, -2, 0 }; // solution(arr); solution3(arr); } public static void solution3(int[] arr) { int[] dp = new int[arr.length]; dp[0] = 1; for (int i = 1; i < arr.length; i++) { for (int j = 0; j < i; j++) { if (arr[i] > arr[j]) { dp[i] = dp[j] + 1; } else { dp[i] = 1; } } } int maxLength = 0; for (int i = 0; i < dp.length; i++) { if (dp[i] > maxLength) { maxLength = dp[i]; } } System.out.println(maxLength); } public static void solution2(int[] arr) { int[] dp = new int[arr.length]; dp[0] = 1; for (int i = 1; i < arr.length; i++) { int temp = 0; for (int j = 0; j < i; j++) { if( (arr[i]>arr[j])){ if(temp<dp[j]) temp = dp[j]; } } dp[i] = temp+1; } int maxLength = 0; for (int i = 0; i < dp.length; i++) { if (dp[i] > maxLength) { maxLength = dp[i]; } } System.out.println(maxLength); } public static void solution1(int[] arr) { int[] dp = new int[arr.length]; dp[0] = 1; int max = arr[0]; for (int i = 1; i < arr.length; i++) { // int max2 =arr[0]; // for(int j = 0;j<i;j++){ // if(arr[j]>max2){ // max2= arr[j]; // } // }\ int temp = 0; for (int j = 0; j < i; j++) { if ((arr[i] > arr[j])) { if (temp < dp[j]) // dp[i]=dp[i-1]+1; temp = dp[j]; } dp[i] = temp + 1; // }else{ // dp[i] = dp[i-1]; // } } // if(arr[i]>max){ // max = arr[i]; // dp[i]=dp[i-1]+1; // // }else{ // dp[i] = dp[i-1]; // } } int maxLength = 0; for (int i = 0; i < dp.length; i++) { if (dp[i] > maxLength) { maxLength = dp[i]; } } System.out.println("maxLength: " + maxLength); } public static void solution(int[] arr) { int[] dp = new int[arr.length]; dp[0] = 1; int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; dp[i] = dp[i - 1] + 1; } else { dp[i] = dp[i - 1]; } } int maxLength = 0; for (int i = 0; i < dp.length; i++) { if (dp[i] > maxLength) { maxLength = dp[i]; } } System.out.println("maxLength: " + maxLength); }}
上面的solution2方法和solution3方法是完整的解决方案,其他的方法也是可以参考得,对于理清思路很有帮助
Reference:
1. http://blog.csdn.net/sdjzping/article/details/8759870
2. http://blog.chinaunix.net/uid-28311809-id-4251024.html
3. http://www.cnblogs.com/handsomecui/p/4692350.html
4. http://blog.163.com/zhaohai_1988/blog/static/20951008520127923814649/
5. http://www.cnblogs.com/pblr/p/5718875.html
6. http://www.cnblogs.com/waytofall/archive/2012/09/10/2678576.html
阅读全文
0 0
- LIS-最长递增子序列的长度-java
- LIS 最长递增子序列 Java实现
- 最长递增子序列 LIS
- 最长递增子序列(LIS)
- 最长递增子序列(LIS)
- 最长递增子序列LIS
- 最长递增子序列LIS
- 最长递增子序列(LIS)
- 最长递增子序列LIS
- 最长递增子序列 LIS
- LIS(最长递增子序列)
- LIS-最长递增子序列
- 最长递增子序列LIS
- 最长递增子序列LIS
- 求最长递增子序列的长度
- 最长递增子序列的长度
- 最长递增子序列的长度
- 【算法】最长递增子序列的长度
- 算法导论 最小生成树 MST-PRIM
- Scanern中的nextLine()和next()
- epoll源码剖析
- 2017.08.02回顾 centos安装anaconda/xgboost linux操作系统信息 top内存CPU notebook注释 feature多
- 超简单理解Android四大组件
- LIS-最长递增子序列的长度-java
- 设计模式之类对象结构型 — BRIDGE (桥接)模式
- Jmeter-BeanShell的使用介绍
- MOOC清华《程序设计基础》第8章:二进制文件的数据存取
- 读取word
- 基于UDP的C/S多播回射服务器
- 微信公众平台 java 处理消息 3
- spring boot 启动错误:Could not resolve placeholder
- 《利用Python进行数据分析》笔记---第4章NumPy基础:数组和矢量计算