|
| 1 | +<h2><a href="https://leetcode.com/problems/k-closest-points-to-origin/">973. K Closest Points to Origina>h2><h3>Mediumh3><hr><p>Given an array of <code>pointscode> where <code>points[i] = [x<sub>isub>, y<sub>isub>]code> represents a point on the <strong>X-Ystrong> plane and an integer <code>kcode>, return the <code>kcode> closest points to the origin <code>(0, 0)code>.p> |
| 2 | + |
| 3 | +<p>The distance between two points on the <strong>X-Ystrong> plane is the Euclidean distance (i.e., <code>√(x<sub>1sub> - x<sub>2sub>)<sup>2sup> + (y<sub>1sub> - y<sub>2sub>)<sup>2sup>code>).p> |
| 4 | + |
| 5 | +<p>You may return the answer in <strong>any orderstrong>. The answer is <strong>guaranteedstrong> to be <strong>uniquestrong> (except for the order that it is in).p> |
| 6 | + |
| 7 | +<p> p> |
| 8 | +<p><strong class="example">Example 1:strong>p> |
| 9 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/03/03/closestplane1.jpg" style="width: 400px; height: 400px;" /> |
| 10 | +<pre> |
| 11 | +<strong>Input:strong> points = [[1,3],[-2,2]], k = 1 |
| 12 | +<strong>Output:strong> [[-2,2]] |
| 13 | +<strong>Explanation:strong> |
| 14 | +The distance between (1, 3) and the origin is sqrt(10). |
| 15 | +The distance between (-2, 2) and the origin is sqrt(8). |
| 16 | +Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. |
| 17 | +We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]]. |
| 18 | +pre> |
| 19 | + |
| 20 | +<p><strong class="example">Example 2:strong>p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:strong> points = [[3,3],[5,-1],[-2,4]], k = 2 |
| 24 | +<strong>Output:strong> [[3,3],[-2,4]] |
| 25 | +<strong>Explanation:strong> The answer [[-2,4],[3,3]] would also be accepted. |
| 26 | +pre> |
| 27 | + |
| 28 | +<p> p> |
| 29 | +<p><strong>Constraints:strong>p> |
| 30 | + |
| 31 | +<ul> |
| 32 | + 1 <= k <= points.length <= 104 |
| 33 | + -104 <= xi, yi <= 104 |
| 34 | +ul> |
0 commit comments