Skip to content

Instantly share code, notes, and snippets.

@shubham1710
Created December 20, 2019 13:20
Show Gist options
  • Save shubham1710/c99e06e7fee5974e2414517aea240958 to your computer and use it in GitHub Desktop.
Save shubham1710/c99e06e7fee5974e2414517aea240958 to your computer and use it in GitHub Desktop.
Ghtw
class Solution {
public:
int lis(const vector<int> &V) {
if (V.size() == 0) return 0;
int longest[V.size() + 1];
int maxLen = 1;
memset(longest, 0, sizeof(longest));
// longest[i] denotes the maximum length of increasing subsequence that ends
// with V[i].
longest[0] = 1;
for (int i = 1; i < V.size(); i++) {
longest[i] = 1;
// V[i] can only come after any V[j] such that V[j] < V[i].
// We try appending V[i] after every such subsequence and update our
// longest[i].
for (int j = 0; j < i; j++) {
if (V[j] < V[i]) longest[i] = max(longest[i], longest[j] + 1);
}
maxLen = max(maxLen, longest[i]);
}
return maxLen;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment