Skip to content

Instantly share code, notes, and snippets.

@trhgquan
Last active October 20, 2019 03:21
Show Gist options
  • Save trhgquan/68fbf8d05cc2f58273e6078702e58d78 to your computer and use it in GitHub Desktop.
Save trhgquan/68fbf8d05cc2f58273e6078702e58d78 to your computer and use it in GitHub Desktop.
For more algorithm, visit https://github.com/trhgquan/CPP
int max(int a, int b) {
return (a > b) ? a : b;
}
int MaximumNonAdj(vector<int> adj) {
int ret = INT_MIN;
adj.insert(adj.begin(), 0);
vector<int> F(adj.size(), 0);
F[1] = max(adj[1], 0);
for (int i = 2; i < adj.size(); ++i) {
F[i] = max(F[i - 2] + adj[i], F[i - 1]);
ret = max(F[i], ret);
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment