leetcode_396. 旋转函数

给定一个长度为 n 的整数数组 A 。

假设 Bk 是数组 A 顺时针旋转 k 个位置后的数组,我们定义 A 的“旋转函数” F 为:

F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]。

计算F(0), F(1), ..., F(n-1)中的最大值。

注意:
可以认为 n 的值小于 105。

示例:

A = [4, 3, 2, 6]

F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26

所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rotate-function
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution:
    def maxRotateFunction(self, A: List[int]) -> int:
        #暴力超时
        def gets(A:List[int])->int:
            n=len(A)
            s=0
            for i in range(n):
                s+=i*A[i]
            return s

        n=len(A)
        if n<=1 :return 0
        res=gets(A)
        for i in range(1,n):
            t=A.pop()
            A.insert(0,t)
            res=max(res,gets(A))
        return res
class Solution:
    def maxRotateFunction(self, A: List[int]) -> int:
        # F(k+1) = F(k) + S - n * Bk[n-k]
        n=len(A)
        s=sum(A)
        if n<=1:return 0
        res=0
        for i in range(n):
            res+=i*A[i]
        t1=res
        for i in range(1,n):
            t2=t1+s-n*A[n-i]
            res=max(t2,res)
            t1=t2
        return res
原文地址:https://www.cnblogs.com/hqzxwm/p/14408730.html