You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
那么我们就找到规律了, F(i) = F(i-1) + sum - n*A[n-i],可以写出代码如下:
class Solution {
public:
int maxRotateFunction(vector<int>& A) {
int t = 0, sum = 0, n = A.size();
for (int i = 0; i < n; ++i) {
sum += A[i];
t += i * A[i];
}
int res = t;
for (int i = 1; i < n; ++i) {
t = t + sum - n * A[n - i];
res = max(res, t);
}
return res;
}
};
Activity