1408. Manhattan Farthest Pair
A delivery company plans two depots on a city grid where vehicles may only drive along streets and avenues, so the travel distance between points (x1, y1) and (x2, y2) is |x1 - x2| + |y1 - y2|. To size its longest route it needs the largest such distance between any two of the given customer locations.
Given points, a list of [x, y] pairs (duplicates are allowed), return the maximum Manhattan distance over all pairs of points as a 64-bit integer. If every point is identical the answer is 0. Checking every pair is far too slow for the largest inputs. Instead rotate the plane with u = x + y and v = x - y: the answer equals max(max u - min u, max v - min v). Coordinates reach 10^9 in magnitude, so differences exceed the 32-bit range. The intended solution runs in O(n) time and O(1) extra space.
Example 1
- Input:
- points = [[0,0],[3,4],[-2,1]]
- Output:
- 8
- Explanation:
The pair (3,4) and (-2,1) is 5 + 3 = 8 apart; (0,0) and (3,4) is 7, and (0,0) to (-2,1) is 3.
Example 2
- Input:
- points = [[5,5],[5,5]]
- Output:
- 0
- Explanation:
Both points coincide, so the farthest distance is 0.
Constraints
2 ≤ points.length ≤ 2 * 105
-109 ≤ x, y ≤ 109
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 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms
Expected complexity
- Time
- O(n)
- Space
- O(1)