package com.fishercoder.solutions;

import java.util.Comparator;
import java.util.PriorityQueue;

public class _973 {

    public static class Solution1 {
        public int[][] kClosest(int[][] points, int K) {
            int[][] ans = new int[K][2];

            PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    double dist1 = getDistance(o1);
                    double dist2 = getDistance(o2);

                    if (dist1 > dist2) {
                        return 1;
                    } else if (dist1 < dist2) {
                        return -1;
                    } else {
                        return 0;
                    }
                }
            });

            for (int[] point : points) {
                pq.add(point);
            }

            for (int i=0; i<K; i++) {
                ans[i] = pq.poll();
            }

            return ans;
        }

        private double getDistance(int[] point) {
            return Math.sqrt(Math.pow(point[0], 2) + Math.pow(point[1], 2));
        }
    }
}

@@ -91,16 +91,1001 @@
 Queue;%0A%0A
+/**%0A * 973. K Closest Points to Origin%0A *%0A * We have a list of points on the plane.  Find the K closest points to the origin (0, 0).%0A *%0A * (Here, the distance between two points on a plane is the Euclidean distance.)%0A *%0A * You may return the answer in any order.  The answer is guaranteed to be unique (except for the order that it is in.)%0A *%0A *%0A *%0A * Example 1:%0A *%0A * Input: points = %5B%5B1,3%5D,%5B-2,2%5D%5D, K = 1%0A * Output: %5B%5B-2,2%5D%5D%0A * Explanation:%0A * The distance between (1, 3) and the origin is sqrt(10).%0A * The distance between (-2, 2) and the origin is sqrt(8).%0A * Since sqrt(8) %3C sqrt(10), (-2, 2) is closer to the origin.%0A * We only want the closest K = 1 points from the origin, so the answer is just %5B%5B-2,2%5D%5D.%0A * Example 2:%0A *%0A * Input: points = %5B%5B3,3%5D,%5B5,-1%5D,%5B-2,4%5D%5D, K = 2%0A * Output: %5B%5B3,3%5D,%5B-2,4%5D%5D%0A * (The answer %5B%5B-2,4%5D,%5B3,3%5D%5D would also be accepted.)%0A *%0A *%0A * Note:%0A *%0A * 1 %3C= K %3C= points.length %3C= 10000%0A * -10000 %3C points%5Bi%5D%5B0%5D %3C 10000%0A * -10000 %3C points%5Bi%5D%5B1%5D %3C 10000%0A * */%0A
 public c
@@ -1294,114 +1294,19 @@
 e%3C%3E(
-new Comparator%3Cint%5B%5D%3E() %7B%0A                @Override%0A                public int compare(int%5B%5D o1, int%5B%5D
+(o1,
  o2)
+ -%3E
  %7B%0A 
@@ -1312,36 +1312,32 @@
                 
- 
-   
 double dist1 = g
@@ -1352,20 +1352,16 @@
 ce(o1);%0A
-    
         
@@ -1417,20 +1417,16 @@
         
-  
-  
 if (dist
@@ -1454,28 +1454,24 @@
             
-    
 return 1;%0A  
@@ -1480,28 +1480,24 @@
             
-   
- 
 %7D else if (d
@@ -1528,28 +1528,24 @@
             
-    
 return -1;%0A 
@@ -1543,20 +1543,16 @@
 urn -1;%0A
-    
         
@@ -1584,28 +1584,24 @@
             
-    
 return 0;%0A  
@@ -1602,30 +1602,8 @@
  0;%0A
-                    %7D%0A
     
@@ -1746,14 +1746,18 @@
 nt i
-=
+ = 
 0; i
-%3C
+ %3C 
 K; i

refactor 973