From 1a21daa1e33b3588d454c6861f42f3d27f941e4c Mon Sep 17 00:00:00 2001 From: brendon <36212954+brendonmiranda@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:34:07 +0100 Subject: [PATCH] 1 more aws --- README.md | 2 + src/main/java/aws/GetMaxCharge.java | 75 +++++++++++++++++++++++++ src/test/java/aws/GetMaxChargeTest.java | 37 ++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 src/main/java/aws/GetMaxCharge.java create mode 100644 src/test/java/aws/GetMaxChargeTest.java diff --git a/README.md b/README.md index 2c0bfd8..f9e9a7e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/main/java/aws/GetMaxCharge.java b/src/main/java/aws/GetMaxCharge.java new file mode 100644 index 0000000..4afb73e --- /dev/null +++ b/src/main/java/aws/GetMaxCharge.java @@ -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 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; + + + } + +} diff --git a/src/test/java/aws/GetMaxChargeTest.java b/src/test/java/aws/GetMaxChargeTest.java new file mode 100644 index 0000000..1fedd3d --- /dev/null +++ b/src/test/java/aws/GetMaxChargeTest.java @@ -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(); + input.add(-2); + input.add(4); + input.add(3); + input.add(-2); + input.add(1); + Assertions.assertEquals(4, getMaxCharge.getMaxCharge(input)); + + input = new ArrayList(); + input.add(-2); + input.add(4); + input.add(9); + input.add(1); + input.add(-1); + Assertions.assertEquals(9, getMaxCharge.getMaxCharge(input)); + + input = new ArrayList(); + input.add(-1); + input.add(3); + input.add(2); + Assertions.assertEquals(3, getMaxCharge.getMaxCharge(input)); + } +}