Skip to content

Commit 68b224e

Browse files
refactor 396
1 parent 4ae4aa1 commit 68b224e

File tree

2 files changed

+82
-47
lines changed

2 files changed

+82
-47
lines changed

src/main/java/com/fishercoder/solutions/_396.java

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,61 +25,62 @@ Calculate the maximum value of F(0), F(1), ..., F(n-1).
2525
2626
So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.*/
2727

28-
//F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1]
2928
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;
3346
}
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-
}
4447

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;
4954
}
50-
return sum;
51-
}
5255

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;
5763
}
58-
a[a.length - 1] = first;
59-
return a;
6064
}
6165

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;
7084
}
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));
8485
}
8586
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._396;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _396Test {
10+
private static _396.Solution1 solution1;
11+
private static _396.Solution2 solution2;
12+
private static int[] A;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _396.Solution1();
17+
solution2 = new _396.Solution2();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
A = new int[]{4, 3, 2, 6};
23+
assertEquals(26, solution1.maxRotateFunction(A));
24+
assertEquals(26, solution2.maxRotateFunction(A));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
A = new int[]{1,2,3,4,5,6,7,8,9,10};
30+
assertEquals(330, solution1.maxRotateFunction(A));
31+
assertEquals(330, solution2.maxRotateFunction(A));
32+
}
33+
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy