6 lines Python


  • 0

    SUM is the sum of array A, cur means the current function value F(i).
    We can see that F(i) = F(i-1) + SUM - n * A[n-i], others are pretty straightforward.

    class Solution(object):
        def maxRotateFunction(self, A):
            """
            :type A: List[int]
            :rtype: int
            """
            n, SUM = len(A), sum(A)
            MAX = cur = sum(i*A[i] for i in range(n))
            for i in range(1, n):
                cur += SUM - n * A[n-i]
                MAX = max(MAX, cur)
            return MAX
    

Log in to reply
 

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