最长递增序列个数
public void sxy(){ int[] nums = {1,2,4,3,5,4,7,2}; int length = nums.length; // 结尾长度 int[] dp = new int[length]; // 结尾数目 int[] cnt = new int[length]; int maxLength = 0; int maxLengthIndex = 0; for (int i=0; i<length;i++){ dp[i] = 1; cnt[i] = 1; int count = 1; for(int j=0;j<i;j++){ if (nums[i]>nums[j]){ int newV = dp[j] + 1; if (dp[i] == newV){ count += cnt[j]; }else if (dp[i] < newV){ dp[i] = newV; // 重置count count = cnt[j]; } } } maxLength = Math.max(maxLength, dp[i]); cnt[i] = count; } int result = 0; for (int i =0; i<length;i++){ if (dp[i] == maxLength){ result += cnt[i]; } } System.out.println(result); }
最长递增序列
int[] nums = {1, 3, 2, 7, 4, 9}; int[] dp = new int[nums.length]; dp[0] = 1; int max = -1; int length = nums.length; for (int i = 1; i < length; i++){ for (int j = i - 1; j >= 0; j--) if (nums[i] > nums[j]){ dp[i] = Math.max(dp[j] + 1, dp[i]); } max = Math.max(dp[i], max); } System.out.println(max);