Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 15074d2

Browse files
xdongyanfishercoder1534
authored andcommitted
* add test 606 * edit 396 * edit 396 * 606 add test
1 parent 5d2c0ef commit 15074d2

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,28 @@ private int[] rotate(int[] a) {
5252
a[a.length-1] = first;
5353
return a;
5454
}
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+
}
5572

5673
public static void main(String...strings){
5774
int[] nums = new int[]{4, 3, 2, 6};
5875
_396 test = new _396();
5976
System.out.println(test.maxRotateFunction(nums));
77+
System.out.println(test.maxRotateFunction_1(nums));
6078
}
6179
}

src/test/java/com/fishercoder/_606Test.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static void setup(){
2020
}
2121

2222
@Test
23-
public void test1(){
23+
public void test1() {
2424
t = new TreeNode(1);
2525
t.left = new TreeNode(2);
2626
t.right = new TreeNode(3);
@@ -30,12 +30,21 @@ public void test1(){
3030
}
3131

3232
@Test
33-
public void test2(){
33+
public void test2() {
3434
t = new TreeNode(1);
3535
t.left = new TreeNode(2);
3636
t.right = new TreeNode(3);
3737
t.left.right = new TreeNode(4);
3838
System.out.println("Test2");
3939
assertEquals("1(2()(4))(3)", test.tree2str(t));
4040
}
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+
}
4150
}

0 commit comments

Comments
 (0)