Skip to content

Instantly share code, notes, and snippets.

@silatori
Last active August 27, 2019 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silatori/3ca172158a5e4e21036f6af2363ef99c to your computer and use it in GitHub Desktop.
Save silatori/3ca172158a5e4e21036f6af2363ef99c to your computer and use it in GitHub Desktop.
Longest Increasing Subsequence(LIS) with C#
public static long LowerBound(long[] a, long key)
{
long l = 0;
long r = a.Length;
long mid;
while (l < r)
{
mid = ((r - l) / 2) + l;
if (a[mid] <= key)
l = mid + 1;
else
r = mid;
}
return l;
}
public static long LIS(long[] a)
{
var dp = Enumerable.Range(0, a.Length).Select(x => long.MaxValue).ToArray();
for (int i = 0; i < a.Length; i++)
{
dp[LowerBound(dp, a[i])] = a[i];
}
return LowerBound(dp, long.MaxValue-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment