Concise Java code, 14 lines


  • 0
    J
    
    public int maxRotateFunction(int[] A) {
        int sum = IntStream.of(A).sum();
        int initial = 0;
        int len = A.length;
        for (int i = 0; i < len; i++) {
            initial += i * A[i];
        }
        int max = initial;
        for (int i = len - 1; i >= 0; i--) {
            initial += sum - len * A[i];
            max = Math.max(max, initial);
        }
        return max;
    }
    
    

    No explanation needed.


Log in to reply
 

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