import java.util.*;
class Solution {
public int lengthOfLIS(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
int[] parent = new int[n]; // to reconstruct the sequence
Arrays.fill(dp, 1);
Arrays.fill(parent, -1);
int maxLen = 1;
int lastIndex = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
parent[i] = j;
}
}
if (dp[i] > maxLen) {
maxLen = dp[i];
lastIndex = i;
}
}
// Reconstruct LIS
List<Integer> lis = new ArrayList<>();
while (lastIndex != -1) {
lis.add(nums[lastIndex]);
lastIndex = parent[lastIndex];
}
Collections.reverse(lis);
System.out.println("LIS: " + lis); // 🖨️ Print LIS
return maxLen;
}
}