315. K Closest Points to Origin

MediumHeap

Given an array of points where points[i] = [x_i, y_i] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0). The distance between two points on the X-Y plane is the Euclidean distance (i.e., sqrt((x1 - x2)^2 + (y1 - y2)^2)). You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in).

Examples

Input: [[1,3],[-2,2]] 1

Output: [[-2,2]]

Explanation: (-2,2) has squared distance 8, closer than (1,3) at 10.

Constraints

  • 1 <= k <= points.length <= 10^4
  • -10^4 <= x_i, y_i <= 10^4
Loading...

Run checks all cases above. Submit evaluates all test cases.