criskgl
10/6/2019 - 8:38 AM

K closest points to origin

class Solution {
  public int[][] kClosest(int[][] points, int K) {
    int N = points.length;
    int[] dists = new int[N];
    for (int i = 0; i < N; ++i)
        dists[i] = dist(points[i]);

    Arrays.sort(dists);
    int distK = dists[K-1];

    int[][] ans = new int[K][2];
    int t = 0;
    for (int i = 0; i < N; ++i)
        if (dist(points[i]) <= distK)
            ans[t++] = points[i];
    return ans;
  }

  public int dist(int[] point) {
    return point[0] * point[0] + point[1] * point[1];
  }
}