/** * 输入一个无序的整数数组,请你找到其中最长递增子序列的长度 * 最长递增子序列不一定是连续的 * */
public class LIS {
public static void main(String[] args) {
int[] nums = new int[] { 10, 9,2,5,3,7,101,18};
int res = search(nums);
System.out.println(res);
}
static int search(int[] nums) {
int[] dp = new int[nums.length];
// dp数组值全部初始化为1,dp[i]表示以nums[i]这个数结尾的最长递增子序列的长度
// 最长递增子序列的长度是dp数组中的最大值
Arrays.fill(dp, 1);
//计算出dp数组中每个以nums[i]这个数结尾的最长递增子序列的长度
for (int i = 0; i < nums.length; i ++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
int res = 0;
for (int i = 0; i < dp.length; i++) {
res = Math.max(res, dp[i]);
}
return res;
}
}
还没有评论,来说两句吧...