public class Solution {
    public int maxRotateFunction(int[] A) {
        
        if(A==null||A.length==0){
            return 0;
        }
        int res = Integer.MIN_VALUE;
        int length = A.length;
        for(int i=0;i<length;i++){
            int sum = 0;
            for(int j=0;j<length;j++){
                int c = (i+j)%length;
                sum+=c*A[j];
            }
            res = Math.max(res,sum);
        }
        return res;

    }

    public int maxRotateFunction2(int[] A) {
        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;   
    }
}
@@ -1,12 +1,469 @@
+/*%0AF(k) = 0 * Bk%5B0%5D + 1 * Bk%5B1%5D + ... + (n-1) * Bk%5Bn-1%5D%0AF(k-1) = 0 * Bk-1%5B0%5D + 1 * Bk-1%5B1%5D + ... + (n-1) * Bk-1%5Bn-1%5D%0A       = 0 * Bk%5B1%5D + 1 * Bk%5B2%5D + ... + (n-2) * Bk%5Bn-1%5D + (n-1) * Bk%5B0%5D%0AThen,%0A%0AF(k) - F(k-1) = Bk%5B1%5D + Bk%5B2%5D + ... + Bk%5Bn-1%5D + (1-n)Bk%5B0%5D%0A              = (Bk%5B0%5D + ... + Bk%5Bn-1%5D) - nBk%5B0%5D%0A              = sum - nBk%5B0%5D%0AThus,%0A%0AF(k) = F(k-1) + sum - nBk%5B0%5D%0AWhat is Bk%5B0%5D?%0A%0Ak = 0; B%5B0%5D = A%5B0%5D;%0Ak = 1; B%5B0%5D = A%5Blen-1%5D;%0Ak = 2; B%5B0%5D = A%5Blen-2%5D%0A%0A*/%0A%0A%0A
 public class

Add comment