Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

133. K Closest Points to Origin

MediumDay 39 of 60Heap / Priority Queue

Given a list of points points[i] = [x, y] on a plane and an integer k, return the k points closest to the origin (0, 0) using Euclidean distance. The answer is unique apart from its order, and you may return the points in any order.

Example 1
Input: points = [[1,3],[-2,2]], k = 1
Output: [[-2,2]]
Squared distances are 10 and 8, so [-2,2] is closer.
Example 2
Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[3,3],[-2,4]]
Squared distances are 18, 26 and 20.
Constraints
  • 1 <= k <= points.length <= 10^4
  • -10^4 <= x, y <= 10^4
Hints
Frequently asked at: Meta, Amazon, Apple, LinkedIn
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min