public int maxRotateFunction(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
int n = A.length;
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int curSum = 0;
for (int j = 0; j < n; j++) {
curSum += ((j + i) % n) * A[j];
}
max = Math.max(curSum, max);
}
return max;
}
k = 0; B[0] = A[0];
k = 1; B[0] = A[len-1];
k = 2; B[0] = A[len-2];
...
最后代码是:
int allSum = 0;
int len = A.length;
int F = 0;
for (int i = 0; i < len; i++) {
F += i * A[i];
allSum += A[i];
}
int max = F;
for (int i = len - 1; i >= 1; i--) {
F = F + allSum - len * A[i];
max = Math.max(F, max);
}
return max;