Skip to the code
cf.
+50 XP

#315 Closest Pair of Map Markers

hard

A store-locator map shows markers at whole-number coordinates in points, each [x, y]. Markers that are too close overlap, so the map needs the distance between the two closest markers.

Write closestPairDistance(points) that returns the smallest squared distance (x1 - x2)² + (y1 - y2)² between any two different markers in the list. Two markers may sit at the same coordinates, which gives 0.

A simple check of every pair is O(n²). Aim for O(n log n).

Example 1

Input: points = [[0,0],[3,4],[1,1]]
Output: 2

[0, 0] and [1, 1] are 1² + 1² = 2 apart, closer than any other pair.

Example 2

Input: points = [[5,5],[5,5]]
Output: 0

Two markers at the same spot are 0 apart.

Constraints

  • 2 <= points.length <= 10^5
  • -10^4 <= x, y <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).