核心代码:
if (nums[i] > nums[j] && LIS[j] + 1 > LIS[i]) {
LIS[i] = LIS[j] + 1;
LIS[i] = max{ LIS[i] , LIS[k]+1 }, 0 <= k < i, a[i]>a[k]
public class LIS {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < nums.length; i++) {
nums[i] = sc.nextInt();
}
sc.close();
System.out.println(longestIncreasingSubsequence(nums));
}
public static int longestIncreasingSubsequence(int[] nums) {
int max = 0;
if (nums.length > 0) {
int[] LIS = new int[nums.length];
Arrays.fill(LIS, 1);
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j] && LIS[j] + 1 > LIS[i]) {
LIS[i] = LIS[j] + 1;
}
}
}
for (int i = 1; i < nums.length; ++i)
if (LIS[i] > max)
max = LIS[i];
}
return max;
}
}