Skip to content

Instantly share code, notes, and snippets.

@limitedmage
Created October 4, 2011 03:54
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 limitedmage/1260867 to your computer and use it in GitHub Desktop.
Save limitedmage/1260867 to your computer and use it in GitHub Desktop.
int seq[4040];
int dyn[4040];
int n;
int lis() {
// longest increasing subsequence
// for sequence seq of length n
// O(nlgn)
// for longest decreasing subsequence, multiply elements by -1
// for longest common subsequence with unique elements, map
// to order of first string and do lis
int size = 1;
dyn[0] = seq[0];
for (int i = 1; i < n; i++) {
int curr = seq[i];
int last = dyn[size - 1];
if (last < curr) {
dyn[size] = curr;
size++;
} else if (last > curr) {
int *place = lower_bound(dyn, dyn + size, curr); // binsearch from <algorithm>
if (*place > curr) {
*place = curr;
}
}
}
return size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment