链接:https://leetcode-cn.com/problems/rotate-function/
数学题,
F
(
i
+
1
)
=
F
(
i
)
+
s
u
m
A
−
n
∗
A
[
n
−
i
−
1
]
F(i+1) = F(i)+sumA-n*A[n-i-1]
F(i+1)=F(i)+sumA−n∗A[n−i−1],其中n为数组A中元素个数。
java代码:
class Solution {
public int maxRotateFunction(int[] A) {
if(A.length==0)
return 0;
int f0 = 0;
int sum = 0;
for(int i =0 ;i<A.length;i++)
{
f0+=i*A[i];
sum+=A[i];
}
int ans = f0;
for(int i = 1;i<A.length;i++)
{
f0+=(sum-A.length*A[A.length-i]);
ans = Math.max(ans,f0);
}
return ans;
}
}