File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -52,10 +52,28 @@ private int[] rotate(int[] a) {
52
52
a [a .length -1 ] = first ;
53
53
return a ;
54
54
}
55
+
56
+ //**credit : https://discuss.leetcode.com/topic/58459/java-o-n-solution-with-explanation
57
+ public int maxRotateFunction_1 (int [] A ) {
58
+ int allSum = 0 ;
59
+ int len = A .length ;
60
+ int F = 0 ;
61
+ for (int i = 0 ; i < len ; i ++) {
62
+ F += i * A [i ];
63
+ allSum += A [i ];
64
+ }
65
+ int max = F ;
66
+ for (int i = len - 1 ; i >= 1 ; i --) {
67
+ F = F + allSum - len * A [i ];
68
+ max = Math .max (F , max );
69
+ }
70
+ return max ;
71
+ }
55
72
56
73
public static void main (String ...strings ){
57
74
int [] nums = new int []{4 , 3 , 2 , 6 };
58
75
_396 test = new _396 ();
59
76
System .out .println (test .maxRotateFunction (nums ));
77
+ System .out .println (test .maxRotateFunction_1 (nums ));
60
78
}
61
79
}
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ public static void setup(){
20
20
}
21
21
22
22
@ Test
23
- public void test1 (){
23
+ public void test1 () {
24
24
t = new TreeNode (1 );
25
25
t .left = new TreeNode (2 );
26
26
t .right = new TreeNode (3 );
@@ -30,12 +30,21 @@ public void test1(){
30
30
}
31
31
32
32
@ Test
33
- public void test2 (){
33
+ public void test2 () {
34
34
t = new TreeNode (1 );
35
35
t .left = new TreeNode (2 );
36
36
t .right = new TreeNode (3 );
37
37
t .left .right = new TreeNode (4 );
38
38
System .out .println ("Test2" );
39
39
assertEquals ("1(2()(4))(3)" , test .tree2str (t ));
40
40
}
41
+
42
+ @ Test
43
+ public void test3 () {
44
+ t = new TreeNode (1 );
45
+ t .right = new TreeNode (2 );
46
+ t .right .right = new TreeNode (3 );
47
+ System .out .println ("Test3" );
48
+ assertEquals ("1()(2()(3))" , test .tree2str (t ));
49
+ }
41
50
}
You can’t perform that action at this time.
0 commit comments