[LintCode]Longest Increasing Subsequence
- 此题并非一道标准的dp。尽管大问题的结果依赖小问题的结果(dp的思路),但该问题的最终输出不是最大的问题的结果,而是所有问题中某一个问题的结果(就本题来看,要求值最大)。这一点值得注意。
- lis[i]的含义为:以nums[i]为结尾的最大递增子序列的元素个数。本题最后输出为最大递增子序列的元素数,既lis数组中值最大的元素。
public class Solution {
/**
* @param nums: The integer array
* @return: The length of LIS (longest increasing subsequence)
*/
public int longestIncreasingSubsequence(int[] nums) {
// 2015-05-18
if (nums == null || nums.length == 0) {
return 0;
}
int n = nums.length;
int lis[] = new int[n];
int max = 0;
for (int i = 0; i < n; i++) {
lis[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] >= nums[j]) {
lis[i] = Math.max(lis[i], lis[j] + 1);
}
} // for
max = Math.max(max, lis[i]);
} // for
return max;
}
}
本文深入探讨了长递增子序列问题的特性,详细解释了其核心概念及其解决策略。通过实例分析,展示了如何利用动态规划算法高效求解此类问题,并阐述了算法背后的逻辑和优化技巧。
4380

被折叠的 条评论
为什么被折叠?



