Problem · Array
Best Meeting Point
Learn this problemProblem statement
You are given a rectangular binary grid. Each cell containing 1 is a home, and each cell containing 0 is empty.
Choose any grid cell as a meeting point. A person at (r, c) travels to (x, y) with Manhattan distance |r - x| + |c - y|.
Return the minimum possible sum of travel distances from every home to one meeting point.
Function
minTotalDistance(grid: int[][]) → intExamples
Example 1
grid = [[1,0,0,0,1],[0,0,0,0,0],[0,0,1,0,0]]return = 6Meeting at row 0, column 2 gives distances 2, 2, and 2.
Example 2
grid = [[1,1]]return = 1Either cell is an optimal meeting point.
Constraints
1 <= grid.length, grid[0].length <= 200.- Every cell is
0or1. - The grid contains at least one home.