Created
September 13, 2016 05:34
-
-
Save eclipselu/44e8213d093b00da493062506e10e46e to your computer and use it in GitHub Desktop.
Rotate Function
This file contains hidden or 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
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