finding nearest veg restaurants
package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class amazon1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<List<Integer>> allLocations = new ArrayList<>();
allLocations.add(Arrays.asList(1,2));
allLocations.add(Arrays.asList(3,4));
allLocations.add(Arrays.asList(1,-3));
nearestVegRest(3,allLocations, 1);
List<List<Integer>> allLocations1 = new ArrayList<>();
allLocations1.add(Arrays.asList(2,4));
allLocations1.add(Arrays.asList(3,6));
allLocations1.add(Arrays.asList(2,7));
allLocations1.add(Arrays.asList(5,3));
allLocations1.add(Arrays.asList(1,8));
allLocations1.add(Arrays.asList(7,9));
nearestVegRest(6,allLocations1, 3);
}
static List<List<Integer>> nearestVegRest(int totalRests, List<List<Integer>> allLocations, int numRest)
{
TreeMap<Double,List<Integer>> map = new TreeMap<>();
List<List<Integer>> nearest = new ArrayList<>();
if(numRest <= totalRests)
{
for(int i =0; i<allLocations.size(); i++)
{
List<Integer> inner = allLocations.get(i);
double sum = 0d;
sum = Math.pow(Math.abs(inner.get(0)), 2) + Math.pow(Math.abs(inner.get(1)), 2);
map.put(Math.sqrt(sum), allLocations.get(i));
}
System.out.println("Map:"+map);
int j = 1;
for(Map.Entry<Double, List<Integer>> ele: map.entrySet())
{
if(j <= numRest )
{
nearest.add(ele.getValue());
++j;
}
}
System.out.println("nearest:"+nearest);
}
return nearest;
}
}
output:
Map:{2.23606797749979=[1, 2], 3.1622776601683795=[1, -3], 5.0=[3, 4]}
nearest:[[1, 2]]
Map:{4.47213595499958=[2, 4], 5.830951894845301=[5, 3], 6.708203932499369=[3, 6], 7.280109889280518=[2, 7], 8.06225774829855=[1, 8], 11.40175425099138=[7, 9]}
nearest:[[2, 4], [5, 3], [3, 6]]
package example;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class amazon1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<List<Integer>> allLocations = new ArrayList<>();
allLocations.add(Arrays.asList(1,2));
allLocations.add(Arrays.asList(3,4));
allLocations.add(Arrays.asList(1,-3));
nearestVegRest(3,allLocations, 1);
List<List<Integer>> allLocations1 = new ArrayList<>();
allLocations1.add(Arrays.asList(2,4));
allLocations1.add(Arrays.asList(3,6));
allLocations1.add(Arrays.asList(2,7));
allLocations1.add(Arrays.asList(5,3));
allLocations1.add(Arrays.asList(1,8));
allLocations1.add(Arrays.asList(7,9));
nearestVegRest(6,allLocations1, 3);
}
static List<List<Integer>> nearestVegRest(int totalRests, List<List<Integer>> allLocations, int numRest)
{
TreeMap<Double,List<Integer>> map = new TreeMap<>();
List<List<Integer>> nearest = new ArrayList<>();
if(numRest <= totalRests)
{
for(int i =0; i<allLocations.size(); i++)
{
List<Integer> inner = allLocations.get(i);
double sum = 0d;
sum = Math.pow(Math.abs(inner.get(0)), 2) + Math.pow(Math.abs(inner.get(1)), 2);
map.put(Math.sqrt(sum), allLocations.get(i));
}
System.out.println("Map:"+map);
int j = 1;
for(Map.Entry<Double, List<Integer>> ele: map.entrySet())
{
if(j <= numRest )
{
nearest.add(ele.getValue());
++j;
}
}
System.out.println("nearest:"+nearest);
}
return nearest;
}
}
output:
Map:{2.23606797749979=[1, 2], 3.1622776601683795=[1, -3], 5.0=[3, 4]}
nearest:[[1, 2]]
Map:{4.47213595499958=[2, 4], 5.830951894845301=[5, 3], 6.708203932499369=[3, 6], 7.280109889280518=[2, 7], 8.06225774829855=[1, 8], 11.40175425099138=[7, 9]}
nearest:[[2, 4], [5, 3], [3, 6]]
No comments:
Post a Comment