【leetcode❤python】 396. Rotate Function

#-*- coding: UTF-8 -*-
#超时
#        lenA=len(A)
#        maxSum=[]
#        count=0
#        while count<lenA:
#            tmpSum=0
#            for i in xrange(lenA):
#                tmpSum+=i*A[i-count]
#            maxSum.append(tmpSum)
#            count+=1
#        return max(maxSum)

#AC源码如下
class Solution(object):
    def maxRotateFunction(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        if A==[]:return 0
        sumA=sum(A)
        lenA=len(A)
        preF=0
        for i in xrange(lenA):
            preF+=i*A[i]
        count=1;maxSum=preF
      
        while count<lenA:
            curF=preF+sumA-lenA*A[-count]
            count+=1;preF=curF
            if maxSum<=curF:maxSum=curF
     
        return maxSum


sol=Solution()
print sol.maxRotateFunction([4,3,2,6])

posted @ 2016-11-13 19:22  火金队长  阅读(245)  评论(0编辑  收藏  举报