Skip to content

edit 537 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/com/fishercoder/solutions/_396.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,28 @@ private int[] rotate(int[] a) {
a[a.length-1] = first;
return a;
}

//**credit : https://discuss.leetcode.com/topic/58459/java-o-n-solution-with-explanation
public int maxRotateFunction1(int[] A) {
int allSum = 0;
int len = A.length;
int F = 0;
for (int i = 0; i < len; i++) {
F += i * A[i];
allSum += A[i];
}
int max = F;
for (int i = len - 1; i >= 1; i--) {
F = F + allSum - len * A[i];
max = Math.max(F, max);
}
return max;
}

public static void main(String...strings){
int[] nums = new int[]{4, 3, 2, 6};
_396 test = new _396();
System.out.println(test.maxRotateFunction(nums));
System.out.println(test.maxRotateFunction1(nums));
}
}
14 changes: 13 additions & 1 deletion src/main/java/com/fishercoder/solutions/_537.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,17 @@ private String multiply(String p, String withI) {
return String.valueOf(numberPart * Integer.valueOf(p)) + "i";
}


/* Mathematical Solution
(a + ib) * (x + iy) = ax + by(i^2) + i(bx + ay) = ax - by + i(bx+ay)
credit : https://leetcode.com/problems/complex-number-multiplication/solution/
*/
public String complexNumberMultiply2(String a, String b) {
String[] m = a.split("\\+|i");
String[] n = b.split("\\+|i");
int aFirst = Integer.parseInt(m[0]);
int aSecond = Integer.parseInt(m[1]);
int bFirst = Integer.parseInt(n[0]);
int bSecond = Integer.parseInt(n[1]);
return(aFirst * bFirst - aSecond*bSecond) + "+" + (aFirst * bSecond + aSecond * bFirst) + "i";
}
}
13 changes: 11 additions & 2 deletions src/test/java/com/fishercoder/_606Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void setup(){
}

@Test
public void test1(){
public void test1() {
t = new TreeNode(1);
t.left = new TreeNode(2);
t.right = new TreeNode(3);
Expand All @@ -30,12 +30,21 @@ public void test1(){
}

@Test
public void test2(){
public void test2() {
t = new TreeNode(1);
t.left = new TreeNode(2);
t.right = new TreeNode(3);
t.left.right = new TreeNode(4);
System.out.println("Test2");
assertEquals("1(2()(4))(3)", test.tree2str(t));
}

@Test
public void test3() {
t= new TreeNode(1);
t.right = new TreeNode(2);
t.right.right = new TreeNode(3);
System.out.println("Test3");
assertEquals("1()(2()(3))", test.tree2str(t));
}
}