Skip to content

Commit

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

[Amazon Review Score](https://github.com/brendonmiranda/CrackingTheCodingInterview/blob/main/src/main/java/aws/AmazonReviewScore.java)
[Optimizing Box Weights]()
[Number of Suitable Locations]()

# Chapter 2 | Linked Lists

Expand Down
90 changes: 90 additions & 0 deletions src/main/java/aws/AmazonNumberOfSuitableLocations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package aws;

public class AmazonNumberOfSuitableLocations {

/**
* // 2 * abs(x - center[i])
*/
public static int numberOfSuitableLocationsSearch(int[] center, int d) {

long low = -1000000000L;
long high = 1000000000L;

Long min = null;
Long max = null;

while (low <= high) {

long x = low + (high - low) / 2;

long dis = 0;
for (int c: center) {
dis += 2 * (Math.abs(x - c));
}

if (dis <= d) {
min = x;
high = x - 1;
} else {
low = x + 1;
}

}

if (min == null)
return 0;

low = -1000000000L;
high = 1000000000L;
while (low <= high) {

long x = low + (high - low) / 2;

long dis = 0;
for (int c: center) {
dis += 2 * (Math.abs(x - c));
}

if (dis <= d) {
max = x;
low = x + 1;
} else {
high = x - 1;
}

}

return Integer.parseInt(String.valueOf((max - min) + 1L));
}

// quadratic expensive way of doing the same
public static int numberOfSuitableLocations(int[] center, int d) {

int min = Integer.MIN_VALUE;
int max = Integer.MAX_VALUE;

int count = 0;
for (int x = min; x < max; x++){
long buffer = 0;

for (int c = 0; c < center.length; c++) {
long distance = (2L * (x - center[c]));
if(distance < 0) {
distance = -(distance); // make absolute
}
buffer = buffer + distance;
}
if (buffer <=d){
count++;
}

}

return count;

}




}
50 changes: 50 additions & 0 deletions src/main/java/aws/AmazonPackItems.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package aws;

import java.util.*;

public class AmazonPackItems {

/*
The intersection of A and B is null.
The union of A and B is equal to the original array.
The number of elements in subset A is minimal.
The sum of A's weights is greater than the sum of B's weights.*/
public static int[] packItems(int[] arr) {

Arrays.sort(arr);

int max = arr.length / 2;
int arrSum = 0;
int bufferSum = 0;

final LinkedList<Integer> buffer = new LinkedList<>();
for (int i = 0; i < arr.length; i++) {
arrSum += arr[i];
if (i >= max) {
buffer.add(arr[i]);
bufferSum += arr[i];
}
}

// The sum of A's weights is greater than the sum of B's weights.
// Subset A must be minimal in size.
arrSum = arrSum - bufferSum;
while ((bufferSum - buffer.get(0)) > (arrSum)) {
// The union of A and B is equal to the original array.
Integer i = buffer.remove(0);
arrSum += i;
bufferSum -= i;

}

int[] subA = new int[buffer.size()];
for (int i = 0; i < buffer.size(); i++) {
subA[i] = buffer.get(i);
}

return subA;

}


}

0 comments on commit 7110dc2

Please sign in to comment.