Last active
October 20, 2019 03:21
-
-
Save trhgquan/68fbf8d05cc2f58273e6078702e58d78 to your computer and use it in GitHub Desktop.
For more algorithm, visit https://github.com/trhgquan/CPP
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
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