Python O(n) simple solution just 42ms


  • 0
    F
    class Solution(object):
        def maxRotateFunction(self, A):
            """
            :type A: List[int]
            :rtype: int
            """
            s = sum(A)
            res_list = [0]
            n = len(A)
            res = 0
            for i in A:
                tmp = i*(n-1)-(s-i)+res_list[-1] #tmp is the profit of one movement
                res_list.append(tmp)
            for i,j in enumerate(A):
                res+=i*j
            return res+max(res_list)
    

Log in to reply
 

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