Given an array of integers A and let n to be its length.
Assume Bk to
be an array obtained by rotating the array A k positions clock-wise,
we define a "rotation function" F on A as
follow:
F(k) = 0 * Bk[0]
+ 1 * Bk[1] + ... + (n-1) * Bk[n-1].
Calculate the maximum value of F(0), F(1), ..., F(n-1).
Note:
n is guaranteed to be less than 105.
Example:
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 = 26So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
这道题的旋转数组的意思是将末尾的 i 元素依次旋转到第一个元素前面,即:
F(0) 4,3,2,6
F(1) 6,4,3,2
F(2) 2,6,4,3
F(3) 3,2,6,4
F(4) 4,3,2,6
而递推公式为F(i)=F(i-1)+sum-n*A[n-i]
代码如下:
![]()
本文介绍了一种针对数组的旋转操作,并定义了一个基于这种操作的“旋转函数”。通过实例展示了如何计算不同旋转状态下的函数值,并找出这些值中的最大值。

937

被折叠的 条评论
为什么被折叠?



