1412. Hull Diameter

Hard7.5 points estimatedGeometryTwo PointersSortingMaths

Wireless beacons are placed at the positions in points, each [x, y]. The reach a relay must cover to link the two farthest-apart beacons is set by their squared Euclidean distance. Return the largest squared distance between any two of the beacons as a long, or 0 if all beacons share one position.

Comparing every pair is too slow for the largest inputs. Build the convex hull with the monotone chain, dropping collinear points and duplicates, then walk antipodal pairs with rotating calipers so each hull vertex is paired with the vertex farthest from it. Handle the degenerate hulls with one or two vertices separately. The intended solution runs in O(n log n) time and O(n) space.

Example 1

Input:
points = [[0,0],[3,4]]
Output:
25
Explanation:

The only pair is 3 right and 4 up apart, so the squared distance is 25.

Example 2

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

Both beacons share a position, so the answer is 0.

Example 3

Input:
points = [[0,0],[2,0],[1,5],[1,1]]
Output:
26
Explanation:

The farthest pair is (0, 0) and (1, 5), at squared distance 26.

Constraints

2 ≤ n ≤ 105

-104 ≤ x, y ≤ 104

Points may repeat.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 8,000 msC++ 2,000 msJava 4,000 msJavaScript 4,000 msTypeScript 4,000 ms

Expected complexity

Time
O(n log n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…