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
Visualization
Time & Space Complexity
For each of m×n cells, we calculate distance to each of the friends (up to m×n friends)
Where k is the number of friends, to store their positions
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