My JavaScript Solution


  • 1
    S

    The solution has already been introduced by others, such as: https://discuss.leetcode.com/topic/58459/java-o-n-solution-with-explanation .
    I just used JavaScript to implement it.

    /**
     * @param {number[]} A
     * @return {number}
     */
    var maxRotateFunction = function(A) {
        if(A.length===0) return 0;
    
        var res = [],
           n = A.length,
           sum=0,
           f=0;
          
        for(var i=0;i<n;i++){
              sum += A[i];
              f += i*A[i];
        }
        
        res[0] = f;
        for (var j = 1; j < n; j++) {
            res[j] = res[j-1] + sum - n*A[n-j];
        }
        return Math.max(...res);
    }
    

Log in to reply
 

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