Rotate Function

Problem Id: 396 Difficulty: Medium


Intuition

Solution


class Solution:
    def maxRotateFunction(self, A: List[int]) -> int:
        s = sum(A)
        l = len(A)
        tmp = sum([i * j for i, j in enumerate(A)])
        ans = tmp
        for i in range(l-1, 0, -1):
            tmp += s
            tmp -= l * A[i]
            ans = max(ans, tmp)
        return ans