-
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
d238728
commit 7110dc2
Showing
3 changed files
with
142 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,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; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
} |
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,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; | ||
|
||
} | ||
|
||
|
||
} |