In a Meta-style ranking or retrieval pipeline, you may need to quickly keep only the nearest candidate embeddings or coordinates. Given a list of points on a 2D plane, return the k points closest to the origin (0, 0).
The distance between a point (x, y) and the origin is sqrt(x^2 + y^2). For efficiency, compare points using squared distance x^2 + y^2 instead of computing square roots. You may return the answer in any order.
points: a list of 2-element lists, where each element is [x, y]k: an integerk points from points with the smallest distances to the originExample 1
points = [[1,3],[-2,2]], k = 1[[-2,2]]10 and 8, so [-2,2] is closer.Example 2
points = [[3,3],[5,-1],[-2,4]], k = 2[[3,3],[-2,4]]18, 26, and 20. The two smallest are 18 and 20.1 <= len(points) <= 10^4-10^4 <= x, y <= 10^41 <= k <= len(points)