Skip to content

Instantly share code, notes, and snippets.

@bmuslih
Last active October 27, 2018 15:53
Show Gist options
  • Save bmuslih/79955fe374c3d728bed9e5842d2649df to your computer and use it in GitHub Desktop.
Save bmuslih/79955fe374c3d728bed9e5842d2649df to your computer and use it in GitHub Desktop.
Amazon Problem 2 for OptimalUtilization
import java.util.*;
import java.util.stream.Collectors;
public class AmazonSolution2 {
List<List<Integer>> optimalUtilization(int deviceCapacity,
List<List<Integer>> foregroundAppList,
List<List<Integer>> backgroundAppList) {
List<List<ComplexCapacity>> foundCapacities = new ArrayList<>();
foregroundAppList.forEach(forApp -> foundCapacities.add(getMemUsage(forApp, backgroundAppList)));
List<List<Integer>> exactCapacity = new ArrayList<>();
List<List<Integer>> closeCapacity = new ArrayList<>();
//get values and select the ones inferior to deviceCapacity
foundCapacities.forEach(set -> exactCapacity.addAll(set.parallelStream().filter(e -> e.getMemoryUsage() == deviceCapacity)
.map(ComplexCapacity::getCoordinate).collect(Collectors.toList())));
if(exactCapacity.isEmpty()){
foundCapacities.forEach(set -> closeCapacity.addAll(set.parallelStream().filter(e -> (e.getMemoryUsage() <= deviceCapacity)
&& e.getMemoryUsage() >= ((deviceCapacity+e.getMemoryUsage())/2))
.map(ComplexCapacity::getCoordinate).collect(Collectors.toList())));
return closeCapacity;
} else {
return exactCapacity;
}
}
List<ComplexCapacity> getMemUsage(List<Integer> forApp, List<List<Integer>> backgroundAppList) {
return backgroundAppList.parallelStream().map(backApp ->
new ComplexCapacity(forApp.get(1)+backApp.get(1),
Arrays.asList(forApp.get(0),backApp.get(0)))).
collect(Collectors.toList());
}
}
class ComplexCapacity {
private int memoryUsage;
private List<Integer> coordinate;
public ComplexCapacity(int memoryUsage, List<Integer> coordinate) {
this.memoryUsage = memoryUsage;
this.coordinate = coordinate;
}
public int getMemoryUsage() {
return memoryUsage;
}
public void setMemoryUsage(int memoryUsage) {
this.memoryUsage = memoryUsage;
}
public List<Integer> getCoordinate() {
return coordinate;
}
public void setCoordinate(List<Integer> coordinate) {
this.coordinate = coordinate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment