Short and clean Python solution

  • 0

    Note that values are searched in reversed order (meaning F(0), F(lenA-1), ..., F(1)) to have a simpler expression for the loop. Alternatively we could have A[:0:-1] in the loop and reverse the sum.

    class Solution(object):
        def maxRotateFunction(self, A):
            :type A: List[int]
            :rtype: int
            sumA = sum(A)
            lenA = len(A)
            cur_max = run_sum = sum(i * e for i, e in enumerate(A))
            for number in A[:-1]:
                run_sum += number * lenA - sumA
                cur_max = max(cur_max, run_sum)
            return cur_max if A else 0

Log in to reply

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