Apr 18, 2023, 03:55 am
Hello,
I am having trouble with the Longest Increasing Subsequence algorithm. I am using the code from this resource to try to get the longest increasing subsequence. However, the output is not what I am expecting.
I would appreciate any help in resolving this issue.
Thanks.
I am having trouble with the Longest Increasing Subsequence algorithm. I am using the code from this resource to try to get the longest increasing subsequence. However, the output is not what I am expecting.
Code:
public static int lis(final int[] A)
{
int n = A.length;
int[] dp = new int[n];
int maxlen = 0;
for (int i = 0; i < n; i++)
{
dp[i] = 1;
for (int j = 0; j < i; j++)
{
if (A[i] > A[j] && dp[i] < dp[j] + 1)
dp[i] = dp[j] + 1;
}
maxlen = Math.max(maxlen, dp[i]);
}
return maxlen;
}
I would appreciate any help in resolving this issue.
Thanks.