A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
The optimal meeting point to minimize the total Manhattan distance is at the median of the people’s coordinates. This is because, in 1D, the sum of absolute differences is minimized at the median.
Let’s visualize this in 1D:
1
______A_____P_______B_______
If two people live at A and B, any meeting point P between A and B gives the same total distance: |A-P| + |B-P| = |B-A|. If P is outside [A, B], the total distance increases.
Now, with more people (C, A, B, D):
1
______C_____A_____P_______B______D______
The best meeting point P is between the inner points (A and B), and the total distance is minimized by pairing the outermost points (C with D, A with B, etc.) and summing their distances to the median.
#include<vector>#include<algorithm>usingnamespace std;
classSolution {
public:int minTotalDistance(vector<vector<int>>& grid) {
vector<int> rows, cols;
int m = grid.size(), n = grid[0].size();
for (int i =0; i < m; ++i)
for (int j =0; j < n; ++j)
if (grid[i][j] ==1) rows.push_back(i);
for (int j =0; j < n; ++j)
for (int i =0; i < m; ++i)
if (grid[i][j] ==1) cols.push_back(j);
int rowMedian = rows[rows.size() /2];
int colMedian = cols[cols.size() /2];
int sum =0;
for (int r : rows) sum += abs(r - rowMedian);
for (int c : cols) sum += abs(c - colMedian);
return sum;
}
};
classSolution {
publicintminTotalDistance(int[][] grid) {
int m = grid.length, n = grid[0].length;
List<Integer> rows =new ArrayList<>();
List<Integer> cols =new ArrayList<>();
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (grid[i][j]== 1) rows.add(i);
for (int j = 0; j < n; j++)
for (int i = 0; i < m; i++)
if (grid[i][j]== 1) cols.add(j);
int rowMedian = rows.get(rows.size() / 2);
int colMedian = cols.get(cols.size() / 2);
int sum = 0;
for (int r : rows) sum += Math.abs(r - rowMedian);
for (int c : cols) sum += Math.abs(c - colMedian);
return sum;
}
}
classSolution {
funminTotalDistance(grid: Array<IntArray>): Int {
val m = grid.size
val n = grid[0].size
val rows = mutableListOf<Int>()
val cols = mutableListOf<Int>()
for (i in0 until m)
for (j in0 until n)
if (grid[i][j] ==1) rows.add(i)
for (j in0 until n)
for (i in0 until m)
if (grid[i][j] ==1) cols.add(j)
val rowMedian = rows[rows.size / 2]
val colMedian = cols[cols.size / 2]
var sum = 0for (r in rows) sum += kotlin.math.abs(r - rowMedian)
for (c in cols) sum += kotlin.math.abs(c - colMedian)
return sum
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defminTotalDistance(self, grid: list[list[int]]) -> int:
m, n = len(grid), len(grid[0])
rows, cols = [], []
for i in range(m):
for j in range(n):
if grid[i][j] ==1:
rows.append(i)
for j in range(n):
for i in range(m):
if grid[i][j] ==1:
cols.append(j)
row_median = rows[len(rows)//2]
col_median = cols[len(cols)//2]
return sum(abs(r - row_median) for r in rows) + sum(abs(c - col_median) for c in cols)
⏰ Time complexity: O(mn + k log k), where m and n are the grid dimensions and k is the number of people (1s in the grid). Collecting coordinates is O(mn), and sorting is O(k log k).
🧺 Space complexity: O(k), for storing the row and column indices.