In a Meta location-ranking pipeline, you are given 2D points and need to return the k points closest to the origin (0, 0). Distance is based on Euclidean distance, but you should avoid unnecessary square root operations.
Write a function that takes:
points: a list of points, where each point is a list [x, y]k: an integerReturn a list of k points that have the smallest distance from the origin. The output may be in any order.
Use squared distance x*x + y*y for comparison, since it preserves ordering.
Example 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)k points is acceptable