java.lang.Object
g0901_1000.s0973_k_closest_points_to_origin.Solution

public class Solution extends Object
973 - K Closest Points to Origin.<p>Medium</p> <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane and an integer <code>k</code>, return the <code>k</code> closest points to the origin <code>(0, 0)</code>.</p> <p>The distance between two points on the <strong>X-Y</strong> plane is the Euclidean distance (i.e., <code>\u221a(x<sub>1</sub> - x<sub>2</sub>)<sup>2</sup> + (y<sub>1</sub> - y<sub>2</sub>)<sup>2</sup></code>).</p> <p>You may return the answer in <strong>any order</strong>. The answer is <strong>guaranteed</strong> to be <strong>unique</strong> (except for the order that it is in).</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/03/03/closestplane1.jpg" alt="" /></p> <p><strong>Input:</strong> points = [[1,3],[-2,2]], k = 1</p> <p><strong>Output:</strong> <a href="-2,2">-2,2</a></p> <p><strong>Explanation:</strong></p> <p>The distance between (1, 3) and the origin is sqrt(10).</p> <p>The distance between (-2, 2) and the origin is sqrt(8).</p> <p>Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.</p> <p>We only want the closest k = 1 points from the origin, so the answer is just <a href="-2,2">-2,2</a>.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> points = [[3,3],[5,-1],[-2,4]], k = 2</p> <p><strong>Output:</strong> [[3,3],[-2,4]]</p> <p><strong>Explanation:</strong> The answer [[-2,4],[3,3]] would also be accepted.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= k <= points.length <= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> < x<sub>i</sub>, y<sub>i</sub> < 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • kClosest

      public int[][] kClosest(int[][] points, int k)