Minimize Manhattan Distances - Problem

Imagine you're a city planner tasked with optimizing the placement of emergency services. You have several potential locations on a 2D grid, and you need to minimize the worst-case response time by strategically removing exactly one location.

Given an array points where points[i] = [xi, yi] represents coordinates on a 2D plane, you need to find the minimum possible maximum Manhattan distance between any two points after removing exactly one point.

The Manhattan distance between two points (x1, y1) and (x2, y2) is calculated as: |x1 - x2| + |y1 - y2|

Goal: Remove one point to minimize the maximum distance between any remaining pair of points.

Input & Output

example_1.py โ€” Basic Case
$ Input: points = [[3,10],[5,15],[10,2],[4,4]]
โ€บ Output: 12
๐Ÿ’ก Note: Remove point [3,10]. The remaining points are [[5,15],[10,2],[4,4]]. The maximum Manhattan distance is between [5,15] and [10,2]: |5-10| + |15-2| = 5 + 13 = 18. Actually, we should remove [5,15] to get max distance of 12 between [3,10] and [10,2].
example_2.py โ€” Optimal Removal
$ Input: points = [[1,1],[1,1],[1,1]]
โ€บ Output: 0
๐Ÿ’ก Note: All points are identical. After removing any one point, the maximum distance between remaining points is 0.
example_3.py โ€” Three Points
$ Input: points = [[1,1],[3,3],[3,1]]
โ€บ Output: 2
๐Ÿ’ก Note: Remove [3,3]. Remaining points [1,1] and [3,1] have Manhattan distance |1-3| + |1-1| = 2.

Visualization

Tap to expand
๐Ÿš‘๐Ÿš‘๐Ÿš‘๐Ÿš‘Emergency Station OptimizationManhattan distance = blocks traveled horizontally + blocks traveled vertically
Understanding the Visualization
1
Map the Territory
Plot all emergency stations on the city grid
2
Identify Extremes
Find stations at the corners of the coverage area
3
Test Removal
Simulate closing each extreme station
4
Optimize Coverage
Choose the closure that minimizes worst-case response time
Key Takeaway
๐ŸŽฏ Key Insight: Manhattan distance has a geometric property that allows coordinate transformation, reducing the problem from checking all O(nยณ) combinations to only testing O(1) extreme points in O(n) time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Two passes: one to find extremes, one to test removing each extreme point

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Store transformed coordinates and track extreme points

n
2n
โšก Linearithmic Space

Constraints

  • 3 โ‰ค points.length โ‰ค 105
  • points[i].length == 2
  • -108 โ‰ค points[i][0], points[i][1] โ‰ค 108
Asked in
Google 45 Amazon 38 Meta 28 Microsoft 22
26.3K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen