K Closest Points to Origin
You are given an array points, where points[i] = [x, y] is a point on the plane, and an integer k. Return the k points that are closest to the origin (0, 0).
Distance is the ordinary straight-line (Euclidean) distance, √(x² + y²). The tests guarantee the answer is unique (no tie for the last place), and you may return the points in any order. The same point may appear more than once in the input; each copy counts as a separate point.
Constraints
1 <= k <= points.length <= 10^4-10^4 <= x, y <= 10^4
Examples
Example 1
Input: points = [[3,3],[1,-2],[-4,0]], k = 1 Output: [[1,-2]]
Squared distances are 18, 5 and 16. The point [1, -2] is the closest.
Example 2
Input: points = [[2,2],[-1,4],[0,-3],[5,1]], k = 2 Output: [[2,2],[0,-3]]
Squared distances are 8, 17, 9 and 26, so the two closest are [2, 2] (8) and [0, -3] (9). Any order is accepted.
Example 3
Input: points = [[0,0]], k = 1 Output: [[0,0]]
The origin itself is at distance 0.
JavaScript you'll use: Heaps and priority queues Numbers, Math and integer tricks
/**
* @param {number[][]} points
* @param {number} k
* @return {number[][]}
*/
function kClosest(points, k) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.