396. Rotate Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int maxRotateFunction(int[] nums) {
int sum = 0;
int f = 0;
for (int i = 0; i < nums.length; i++) {
f += i * nums[i];
sum += nums[i];
}
int maxRotate = f;

for (int i = 1; i < nums.length; i++) {
f = f - (nums.length - 1) * nums[nums.length - i] + (sum - nums[nums.length - i]);
maxRotate = Math.max(maxRotate, f);
}

return maxRotate;
}
}

References

396. Rotate Function