Skip to content

Commit a1a4445

Browse files
Contest/src/_20160910_4th_contest/RotateFunction.java
1 parent acb9cf4 commit a1a4445

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package _20160910_4th_contest;
2+
3+
//F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]
4+
public class RotateFunction {
5+
public int maxRotateFunction(int[] A) {
6+
if(A == null || A.length == 0) return 0;
7+
int[] F = new int[A.length];
8+
int[] B = A;
9+
int max = Integer.MIN_VALUE;
10+
for(int i = 0; i < A.length; i++){
11+
F[i] = compute(B);
12+
max = Math.max(max, F[i]);
13+
B = rotate(B);
14+
}
15+
return max;
16+
}
17+
18+
private int compute(int[] b) {
19+
int sum = 0;
20+
for(int i = 0; i < b.length; i++){
21+
sum += i*b[i];
22+
}
23+
return sum;
24+
}
25+
26+
private int[] rotate(int[] a) {
27+
int first = a[0];
28+
for(int i = 1; i < a.length; i++){
29+
a[i-1] = a[i];
30+
}
31+
a[a.length-1] = first;
32+
return a;
33+
}
34+
35+
public static void main(String...strings){
36+
int[] nums = new int[]{4, 3, 2, 6};
37+
RotateFunction test = new RotateFunction();
38+
System.out.println(test.maxRotateFunction(nums));
39+
}
40+
}

0 commit comments

Comments
 (0)