@@ -25,61 +25,62 @@ Calculate the maximum value of F(0), F(1), ..., F(n-1).
25
25
26
26
So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.*/
27
27
28
- //F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]
29
28
public class _396 {
30
- public int maxRotateFunction (int [] A ) {
31
- if (A == null || A .length == 0 ) {
32
- return 0 ;
29
+ public static class Solution1 {
30
+ /**
31
+ * F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]
32
+ */
33
+ public int maxRotateFunction (int [] A ) {
34
+ if (A == null || A .length == 0 ) {
35
+ return 0 ;
36
+ }
37
+ int [] F = new int [A .length ];
38
+ int [] B = A ;
39
+ int max = Integer .MIN_VALUE ;
40
+ for (int i = 0 ; i < A .length ; i ++) {
41
+ F [i ] = compute (B );
42
+ max = Math .max (max , F [i ]);
43
+ B = rotate (B );
44
+ }
45
+ return max ;
33
46
}
34
- int [] F = new int [A .length ];
35
- int [] B = A ;
36
- int max = Integer .MIN_VALUE ;
37
- for (int i = 0 ; i < A .length ; i ++) {
38
- F [i ] = compute (B );
39
- max = Math .max (max , F [i ]);
40
- B = rotate (B );
41
- }
42
- return max ;
43
- }
44
47
45
- private int compute (int [] b ) {
46
- int sum = 0 ;
47
- for (int i = 0 ; i < b .length ; i ++) {
48
- sum += i * b [i ];
48
+ private int compute (int [] b ) {
49
+ int sum = 0 ;
50
+ for (int i = 0 ; i < b .length ; i ++) {
51
+ sum += i * b [i ];
52
+ }
53
+ return sum ;
49
54
}
50
- return sum ;
51
- }
52
55
53
- private int [] rotate (int [] a ) {
54
- int first = a [0 ];
55
- for (int i = 1 ; i < a .length ; i ++) {
56
- a [i - 1 ] = a [i ];
56
+ private int [] rotate (int [] a ) {
57
+ int first = a [0 ];
58
+ for (int i = 1 ; i < a .length ; i ++) {
59
+ a [i - 1 ] = a [i ];
60
+ }
61
+ a [a .length - 1 ] = first ;
62
+ return a ;
57
63
}
58
- a [a .length - 1 ] = first ;
59
- return a ;
60
64
}
61
65
62
- //**credit : https://discuss.leetcode.com/topic/58459/java-o-n-solution-with-explanation
63
- public int maxRotateFunctionV2 (int [] A ) {
64
- int allSum = 0 ;
65
- int len = A .length ;
66
- int F = 0 ;
67
- for (int i = 0 ; i < len ; i ++) {
68
- F += i * A [i ];
69
- allSum += A [i ];
66
+ public static class Solution2 {
67
+ /**
68
+ * Reference: https://discuss.leetcode.com/topic/58459/java-o-n-solution-with-explanation
69
+ */
70
+ public int maxRotateFunction (int [] A ) {
71
+ int allSum = 0 ;
72
+ int len = A .length ;
73
+ int F = 0 ;
74
+ for (int i = 0 ; i < len ; i ++) {
75
+ F += i * A [i ];
76
+ allSum += A [i ];
77
+ }
78
+ int max = F ;
79
+ for (int i = len - 1 ; i >= 1 ; i --) {
80
+ F = F + allSum - len * A [i ];
81
+ max = Math .max (F , max );
82
+ }
83
+ return max ;
70
84
}
71
- int max = F ;
72
- for (int i = len - 1 ; i >= 1 ; i --) {
73
- F = F + allSum - len * A [i ];
74
- max = Math .max (F , max );
75
- }
76
- return max ;
77
- }
78
-
79
- public static void main (String ... strings ) {
80
- int [] nums = new int []{4 , 3 , 2 , 6 };
81
- _396 test = new _396 ();
82
- System .out .println (test .maxRotateFunction (nums ));
83
- System .out .println (test .maxRotateFunctionV2 (nums ));
84
85
}
85
86
}
0 commit comments