无序数组中最长非降子序列长度
来源:互联网 发布:域名对企业的意义 编辑:程序博客网 时间:2024/06/05 10:48
求无序整型数组中最长非降子序列的长度。
例如:数组 array[12]={8,9,1,100,2,3,4,5,1001,5,200,1000}中最长非降子序列为:1,2,3,4,5,5,200,1000。长度为8。
解法:动态规划
状态转移方程:dp[i]=1 (array[i]小于前面的所有元素时)
dp[i]=max(dp[j])+1 (0<=j<=i-1 && array[i]>=array[j])。
dp[i]中存的是以array[i]结尾的最长非降子序列的长度。
#include<iostream>#include<string.h>int MaxLength(int array[],int length);int main(){ int array[12]={8,9,1,100,2,3,4,5,1001,5,200,1000}; int maxLength=MaxLength(array,12); std::cout<<maxLength<<std::endl; return 0;}int MaxLength(int array[],int length){ if(array == NULL || length <= 0) return -1; int result = 0; int maxLen = 0; int dp[length]; memset(dp,0,sizeof(int)*length); dp[0] = 1; for(int i=1;i < length;i++) { maxLen=0; for(int j=i-1;j>=0;j--) { if(array[i]>=array[j] && dp[j]>maxLen) { maxLen=dp[j]; } } dp[i] = maxLen + 1; if(result<dp[i]) { result = dp[i]; } } return result;}
0 0
- 无序数组中最长非降子序列长度
- 无序数组中最长连续序列
- 无序数组中找到最长连续子序列
- 无序整形数组,如何找到最长连续序列长度,时间复杂度O(n)
- 无序数组的最长递增子序列
- 求数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- 数组中最长递增子序列的长度
- 求数组中最长递增子序列的长度
- O(n)时间找出无序数组中最长的连续递增序列
- LIS:最长非降子序列长度
- 一个无序整数数组中找到最长连续序列(Longest Consecutive Sequence)和两个元素使得相差最小
- 数组中最短无序序列
- 数组中最长连续序列
- 一维数组中最长递增子序列的长度(DP做法)
- lucene学习--lucene基本概念和流程
- 51nod -1090 . 3个数和为0 && 1267 . 4个数和为0
- .Net线程的一些问题
- linux安装nignx
- HDU 5047 Sawtooth(大数优化+递推公式)
- 无序数组中最长非降子序列长度
- OpenCart 2.0 Beta版本发布及其新特性简介
- Axure RP Extension for Chrome安装
- SAP HR技术系列之一:e-HR,不只是看起来很美
- kindle无缝迁移笔记
- jquery 相册一下滑动两次
- 【BZOJ3629】[JLOI2014]聪明的燕姿 数学+搜索
- GDI泄露终极解决方案——HOOK API,建立GDI对象引用计数器
- 互联网那一边最好的