Last active
August 27, 2019 15:46
-
-
Save silatori/3ca172158a5e4e21036f6af2363ef99c to your computer and use it in GitHub Desktop.
Longest Increasing Subsequence(LIS) with C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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