Skip to content

Commit

Permalink
1 more aws
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Oct 12, 2024
1 parent fde260b commit 1a21daa
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ This repo register my evolution in the Cracking The Coding Interview book as I g

[Number of Suitable Locations](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/aws/NumberOfSuitableLocations.java)

[Get Max Charge]()

# Chapter 2 | Linked Lists

[**2.1** Remove Dups: Write code to remove duplicates from an unsorted linked list.](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/cracking/the/code/interview/chapter2/RemoveDupQuestion21.java)
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/aws/GetMaxCharge.java
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;


}

}
37 changes: 37 additions & 0 deletions src/test/java/aws/GetMaxChargeTest.java
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));
}
}

0 comments on commit 1a21daa

Please sign in to comment.