Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.3 kB
24
Indexable
Never
public class Solution {
    // DO NOT MODIFY THE LIST. IT IS READ ONLY
    public ArrayList<ArrayList<Integer>> solve(final List<ArrayList<Integer>> A, final int B) {
        int n = A.size();
        ArrayList<ArrayList<Integer>> res = new ArrayList<>();
        
        for(int i = 0; i < n; i++){
            ArrayList<Integer> temp = new ArrayList<>();
            temp = A.get(i);
            res.add(temp);
        }
        
        Collections.sort(res, new Comparator<ArrayList<Integer>>(){
            @Override
            public int compare(ArrayList<Integer> point1, ArrayList<Integer> point2){
                int x1 = point1.get(0), y1 = point1.get(1);
                int x2 = point2.get(0), y2 = point2.get(1);
                
                int dist1 = (x1 * x1) + (y1 * y1);
                int dist2 = (x2 * x2) + (y2 * y2);
                return (-1 * Integer.compare(dist1, dist2));
            }
        });
        
        ArrayList<ArrayList<Integer>> finalPoints = new ArrayList<>();
        for(int i = 0; i < B; i++){
            ArrayList<Integer> temp = new ArrayList<>();
            temp = res.get(i);
            finalPoints.add(temp);
        }
        
        return finalPoints;
    }
}


Leave a Comment