public class Main {
public static void getLongestIncrSub(int[] arr) {
int len = arr.length;
int[] dp = new int[len];
int[] res = new int[len];
Arrays.fill(dp, 1);
Arrays.fill(res, 0);
int maxNum = 0;
for (int i = 0; i < len; i++) {
maxNum = Math.max(maxNum, arr[i]);
for (int j = i; j >= 0; j--) {
if (arr[i] > arr[j]) {
dp[i] = 1 + dp[j];
res[i] = arr[j];
break;
}
}
}
int maxSubNum = 0;
for (int dpI : dp) {
maxSubNum = Math.max(dpI, maxSubNum);
}
HashSet<Integer> subNumbers = new HashSet<>();
for (int resI : res) {
if (resI == 0) {
continue;
}
subNumbers.add(resI);
}
subNumbers.add(maxNum);
System.out.println(Arrays.toString(dp));
System.out.println("the longest incr sub number is: " + maxSubNum);
System.out.println(Arrays.toString(res));
System.out.println("the longest incr sub numbers is: " + subNumbers);
}
public static void main(String[] args) {
int[] arr = {2, 1, 5, 3, 6, 4, 8, 9, 7};
getLongestIncrSub(arr);
}
}