Saturday, July 14, 2012
Longest Increasing Sequence C#
static public int LongestIncreasingSeq(int[] s) {
int[] l = new int[s.Length]; // DP table for max length[i]
int[] p = new int[s.Length]; // DP table for predeccesor[i]
int max = int.MinValue;
l[0] = 1;
for (int i=0; i<s.Length; i++)
p[i] = -1;
for (int i = 1; i < s.Length; i++)
{
l[i] = 1;
for (int j = 0; j < i; j++)
{
if (s[j] < s[i] && l[j] + 1 > l[i])
{
l[i] = l[j] + 1;
p[i] = j;
if (l[i] > max)
max = l[i];
}
}
}
return max;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment