|
| 1 | +# Best Meeting Point |
| 2 | + |
| 3 | +You are given a 2D grid of size m×n, where each cell contains either a 0 or a 1. A 1 represents the home of a friend, |
| 4 | +and a 0 represents an empty space. |
| 5 | + |
| 6 | +Your task is to return the minimum total travel distance to a meeting point. The total travel distance is the sum of the |
| 7 | +Manhattan distances between each friend’s home and the meeting point. |
| 8 | + |
| 9 | +The **Manhattan Distance** between two points `(x1, y1)` and `(x2, y2)` is calculated as: |
| 10 | +`|x2 - x1| + |y2 - y1|`. |
| 11 | + |
| 12 | +## Constraints |
| 13 | + |
| 14 | +- m == grid.length |
| 15 | +- n == grid[i].length |
| 16 | +- 1 ≤ m, n ≤ 50 |
| 17 | +- `grid[i][j]` is either 0 or 1. |
| 18 | +- There will be at least two friends in the grid. |
| 19 | + |
| 20 | +## Examples |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +The main idea of this algorithm is that the total Manhattan distance is minimized when all friends meet at the median |
| 29 | +position, calculated separately for rows and columns. As Manhattan distance can be split into vertical and horizontal |
| 30 | +components, we collect all the row indices and column indices of the friends and compute the distance to their respective |
| 31 | +medians. As we loop through the grid row-wise and column-wise, the row and column indices are gathered in sorted order |
| 32 | +naturally, so no additional sorting is needed. Finally, a two-pointer approach is used to efficiently compute the total |
| 33 | +distance by pairing positions from both ends toward the center. |
| 34 | + |
| 35 | +Using the intuition above, we implement the algorithm as follows: |
| 36 | + |
| 37 | +1. Create two vectors, `rows` and `cols`, to store all cells’ row and column indexes where `grid[i][j] == 1`. |
| 38 | +2. Iterate through the grid row by row. For each cell that contains a 1, push the row index `i` into the `rows` vector. |
| 39 | +3. Iterate through the grid column by column. For each cell that contains a `1`, push the column index j into the `cols` vector. |
| 40 | +4. Use the helper function getMinDistance(rows) to calculate the total vertical distance to the optimal row (median). |
| 41 | +5. Use the helper function getMinDistance(cols) to calculate the total horizontal distance to the optimal column (median). |
| 42 | +6. Return the sum of the two distances as the minimum total travel distance. |
| 43 | + |
| 44 | +The getMinDistance helper function receives a list of positions, points, and returns the total minimum travel distance |
| 45 | +to the median. The points list contains either row or column indices of friends. As the Manhattan distance is minimized |
| 46 | +at the median, it uses a two-pointer technique as follows: |
| 47 | + |
| 48 | +- Initialize a variable, distance, with 0 to compute the total distance. |
| 49 | +- Initialize two pointers, i and j, one at the start and the other at the end. |
| 50 | +- Each step adds the difference points[j] - points[i] to the total distance. |
| 51 | +- This process continues until the pointers meet. |
| 52 | +- Returns the total computed distance. |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +### Time Complexity |
| 69 | + |
| 70 | +The time complexity of the above algorithm is `O(m×n+k)`, where m×n are the dimensions of the grid and k is the number |
| 71 | +of friends (number of 1s in the grid). This is because: |
| 72 | + |
| 73 | +- O(m×n) to traverse the entire grid and collect row and column indices. |
| 74 | +- O(k) to compute distances in getMinDistance, where k is the number of friends. |
| 75 | + |
| 76 | +### Space Complexity |
| 77 | + |
| 78 | +The algorithm’s space complexity is `O(k)` because we store up to k row indices and k column indices in two separate |
| 79 | +vectors. |
0 commit comments