easy java solution


  • 0
    F

    The key point is to maintain that:
    sum2=sum1+a0+a1+...+a(n-2)-(n-1)a(n-1)

        public int maxRotateFunction(int[] A) {
            int n = A.length;
            int sum = 0;
            int current = 0;
            for (int i = 0; i < n; i++) {
                sum += A[i];
                current += i * A[i];
            }
            int result = current;
            for (int i = n - 1; i > 0; i--) {
                current += sum - n * A[i];
                result = Math.max(result, current);
            }
            return result;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.