Skip to content

Instantly share code, notes, and snippets.

@eclipselu
Created September 13, 2016 05:34
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 eclipselu/44e8213d093b00da493062506e10e46e to your computer and use it in GitHub Desktop.
Save eclipselu/44e8213d093b00da493062506e10e46e to your computer and use it in GitHub Desktop.
Rotate Function
class Solution {
public:
int maxRotateFunction(vector<int>& A) {
int len = A.size();
int sum = 0;
int psum = 0;
// sum
for (int i = 0; i < len; ++i) {
sum += A[i];
psum += i * A[i];
}
int result = psum;
for (int n = 1; n < len; ++n) {
psum = psum + sum - len * A[len - n];
result = max(psum, result);
}
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment