Practice/Meta/Leetcode 973. K Closest Points to Origin
CodingMust
You are given an array of coordinate pairs representing points on a 2D plane, along with an integer k. Your task is to identify and return the k points that are closest to the origin (0, 0).
The distance between a point (x, y) and the origin is calculated using the Euclidean distance formula: sqrt(x² + y²). The returned points can be in any order.
Example 1:
Input: points = [[1,3],[-2,2]], k = 1 Output: [[-2,2]] Explanation: Distance from [1,3] to origin = sqrt(1² + 3²) = sqrt(10) ≈ 3.16 Distance from [-2,2] to origin = sqrt(4 + 4) = sqrt(8) ≈ 2.83 [-2,2] is closer to the origin.
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], k = 2 Output: [[3,3],[-2,4]] Explanation: The two points with smallest distances are [3,3] (distance ≈ 4.24) and [-2,4] (distance ≈ 4.47). The order in the output doesn't matter.
Example 3:
Input: points = [[1,1],[2,2],[3,3]], k = 3 Output: [[1,1],[2,2],[3,3]] Explanation: When k equals the total number of points, return all points.