Created
December 5, 2019 13:21
-
-
Save criskgl/792200f764e89bed8f31f8ed339b2425 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment