-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fde260b
commit 1a21daa
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package aws; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
public class GetMaxCharge { | ||
|
||
private class Node { | ||
Node left; | ||
Node right; | ||
Integer value; | ||
} | ||
|
||
public long getMaxCharge(List<Integer> charge) { | ||
|
||
Node root = new Node(); | ||
|
||
Node child = root; | ||
Node prev = null; | ||
for (int i = 0; i < charge.size(); i++) { | ||
child.left = prev; | ||
child.value = charge.get(i); | ||
prev = child; | ||
|
||
if (!(i == charge.size() - 1)) { | ||
child.right = new Node(); | ||
child = child.right; | ||
} | ||
} | ||
|
||
charge.sort(Comparator.naturalOrder()); | ||
|
||
for (int i = 0; i < charge.size() - 1; i++) { | ||
|
||
child = root; | ||
while (child.value != charge.get(i)) { | ||
child = child.right; | ||
if (child == null) | ||
break; | ||
} | ||
if (child == null) | ||
continue; | ||
|
||
if (child.left != null | ||
&& child.right != null) { | ||
child.left.value += child.right.value; | ||
|
||
child.right.left = child.left; | ||
child.left.right = child.right; | ||
child = child.left; | ||
|
||
child.right = child.right.right; | ||
|
||
if(child.right != null) | ||
child.right.left = child; | ||
continue; | ||
} | ||
|
||
|
||
if (child.left == null) { | ||
root = child.right; | ||
root.left = null; | ||
} else if (child.right == null) { | ||
child = child.left; | ||
child.right = null; | ||
} | ||
|
||
} | ||
|
||
return root.value; | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package aws; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class GetMaxChargeTest { | ||
|
||
@Test | ||
public void test() { | ||
|
||
GetMaxCharge getMaxCharge = new GetMaxCharge(); | ||
|
||
var input = new ArrayList<Integer>(); | ||
input.add(-2); | ||
input.add(4); | ||
input.add(3); | ||
input.add(-2); | ||
input.add(1); | ||
Assertions.assertEquals(4, getMaxCharge.getMaxCharge(input)); | ||
|
||
input = new ArrayList<Integer>(); | ||
input.add(-2); | ||
input.add(4); | ||
input.add(9); | ||
input.add(1); | ||
input.add(-1); | ||
Assertions.assertEquals(9, getMaxCharge.getMaxCharge(input)); | ||
|
||
input = new ArrayList<Integer>(); | ||
input.add(-1); | ||
input.add(3); | ||
input.add(2); | ||
Assertions.assertEquals(3, getMaxCharge.getMaxCharge(input)); | ||
} | ||
} |