Skip to content
Decoder

K Closest Points to the Origin

Medium

Problem

Given a list of points points, where each point is [x, y], and a number k, return the k points closest to the origin (0, 0) by straight-line distance. You can return them in any order.

Example 1

Input
points = [[1, 3], [-2, 2]], k = 1
Output
[[-2, 2]]

Example 2

Input
points = [[3, 3], [5, -1], [-2, 4]], k = 2
Output
[[3, 3], [-2, 4]]

Constraints

  • 1 <= k <= points.length <= 10^4
  • -10^4 <= x, y <= 10^4
  • The tests are chosen so the answer is unique.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.