Given an array of integers A and let n to be its length.

Assume $B_k$ 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 $10^5$.

Example:

1
2
3
4
5
6
7
8
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
So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.


暴力法

这个没啥好说的,时间复杂度O($n^2$), 超时了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public int maxRotateFunction(int[] A) {
if (A == null || A.length == 0)
return 0;
int n = A.length;
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) {
sum += j * A[i];
i = (i + 1) % n;
}
max = Math.max(sum, max);
}
return max;
}

递推公式,数学

这个要用到数学推到,请参考链接:https://discuss.leetcode.com/topic/58459/java-o-n-solution-with-explanation. 时间复杂度为O(n)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int maxRotateFunction(int[] A) {
int allSum = 0;
int len = A.length;
int F = 0;
for (int i = 0; i < len; i++) {
F += i * A[i];
allSum += A[i];
}
int max = F;
for (int i = len - 1; i >= 1; i--) {
F = F + allSum - len * A[i];
max = Math.max(F, max);
}
return max;
}

递推公式,更好理解

https://discuss.leetcode.com/topic/58616/java-solution-o-n-with-non-mathametical-explaination

把Discuss里的解释贴在这里:

Consider we have 5 coins A,B,C,D,E

According to the problem statement
F(0) = (0A) + (1B) + (2C) + (3D) + (4E)
F(1) = (4A) + (0B) + (1C) + (2D) + (3E)
F(2) = (3A) + (4B) + (0C) + (1D) + (2*E)

This problem at a glance seem like a difficult problem. I am not very strong in mathematics, so this is how I visualize this problem

We can construct F(1) from F(0) by two step:

  1. taking away one count of each coin from F(0), this is done by subtracting “sum” from “iteration” in the code below
    after step 1 F(0) = (-1A) + (0B) + (1C) + (2D) + (3*E)

  2. Add n times the element which didn’t contributed in F(0), which is A. This is done by adding “A[j-1] * len” in the code below.
    after step 2 F(0) = (4A) + (0B) + (1C) + (2D) + (3E)

At this point F(0) can be considered as F(1) and F(2) to F(4) can be constructed by repeating the above steps.

Hope this explanation helps, cheers!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public int maxRotateFunction(int[] A) {
if (A.length == 0) {
return 0;
}
int sum = 0, iteration = 0, len = A.length;
for (int i = 0; i < len; i++) {
sum += A[i];
iteration += (A[i] * i);
}
int max = iteration;
for (int j = 1; j < len; j++) {
// for next iteration lets remove one entry value of each entry and the prev 0 * k
iteration = iteration - sum + A[j - 1] * len;
max = Math.max(max, iteration);
}
return max;
}