题目

https://leetcode-cn.com/problems/rotate-function/

代码

class Solution {
    public int maxRotateFunction(int[] nums) {
        int n=nums.length;
        if(n==1)    return 0;
        int sum=0;
        int f=0;
        for(int i=0;i<n;i++){
            sum+=nums[i];
            f+=i*nums[i];
        }
        // f(n)=f(n-1)-sum+n*nums[n-1]
        int max=f;
        for(int i=1;i<n;i++){
            f+=n*nums[i-1]-sum;
            max=Math.max(max,f);
        }
        return max;
    }
}