Best Meeting Point - Problem

Imagine you and your friends live scattered across a city grid, and you want to find the perfect meeting point that minimizes everyone's total travel time!

Given an m × n binary grid where each 1 represents a friend's house, find the location that minimizes the total Manhattan distance for everyone to reach.

Manhattan Distance between two points is calculated as: |x₁ - x₂| + |y₁ - y₂|

Your goal is to return the minimal total travel distance when all friends meet at the optimal point.

Example: If friends live at (0,0), (0,4), and (2,2), the best meeting point might be (1,2) with total distance = 3 + 1 + 2 = 6

Input & Output

example_1.py — Basic Case
$ Input: grid = [[1,0,0,0,1],[0,0,0,0,0],[0,0,1,0,0]]
Output: 6
💡 Note: Friends are at (0,0), (0,4), and (2,2). The optimal meeting point is (0,2) with distances: 2 + 2 + 2 = 6.
example_2.py — Single Friend
$ Input: grid = [[1]]
Output: 0
💡 Note: Only one friend, so the optimal meeting point is at their location with total distance 0.
example_3.py — Linear Arrangement
$ Input: grid = [[1,1,0,0,1]]
Output: 4
💡 Note: Friends at (0,0), (0,1), (0,4). Median is at column 1, so meeting point (0,1) gives distances: 1 + 0 + 3 = 4.

Visualization

Tap to expand
🎯 Best Meeting Point VisualizationCity Grid with FriendsAlice(0,0)Bob(0,4)Carol(2,2)🍽️Restaurant(0,2)Median PropertyX-coordinates: [0, 0, 2] → Median: 0Y-coordinates: [0, 4, 2] → Median: 2Optimal Meeting Point: (0, 2)Distance from Alice: |0-0| + |0-2| = 2Distance from Bob: |0-0| + |4-2| = 2Distance from Carol: |2-0| + |2-2| = 2Total Distance: 6Why Median Works🔑 Key Insight: Manhattan distance = |x₁-x₂| + |y₁-y₂|• We can optimize X and Y coordinates independently• For any dimension, the median minimizes sum of absolute deviations• This gives us O(mn) solution instead of O(m²n²) brute force• Complexity: O(mn) time, O(k) space where k = number of friends🎯 Result: Minimum total walking distance is 6 blocks!
Understanding the Visualization
1
Identify Friend Locations
Scan the grid to find all houses marked with 1
2
Separate Dimensions
Manhattan distance allows us to solve X and Y independently
3
Find Medians
The median coordinate minimizes total distance in each dimension
4
Calculate Total
Sum all Manhattan distances from friends to the median point
Key Takeaway
🎯 Key Insight: Manhattan distance allows independent optimization of X and Y coordinates using the median property, reducing complexity from O(m²n²) to O(mn).

Time & Space Complexity

Time Complexity
⏱️
O(m²n²)

For each of m×n cells, we calculate distance to each of the friends (up to m×n friends)

n
2n
Quadratic Growth
Space Complexity
O(k)

Where k is the number of friends, to store their positions

n
2n
Linear Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 200
  • grid[i][j] is either 0 or 1
  • There will be at least one friend in the grid
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 25 Apple 18
52.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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