leetcode Longest Increasing Subsequence 最长递增子序列

Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101],
therefore the length is 4. 
there may be more than one LIS combination, 
it is only necessary to return the length.
思路:动态规划
测试数据v[6]={523867}
用d[i]来记录v[i]为结尾的最大递增子序列的长度,初始化1
对于每一个i,令j从1到i - 1遍历,当v[j] < v[i],
比较当前d[i]和每一个d[j] + 1的大小,将最大值赋给d[i]。

第一个数字5,最大递增子序列的长度d[0] = 1
第二个数字2,前面没有比他还小的了,d[1] = 1
第三个数字3,前面2比它小,d[2]=max(d[2],d[1]+1)=2
第四个数组8,d[3]=max(d[3],d[1]+1)=2,d[3]=max(d[3],d[2]+1)=3
第五个数字6,d[4] = 3
第六个数字7,d[5] = 4

猜你喜欢

转载自blog.csdn.net/acttell/article/details/80810401
今日推荐