05 August 2008
最佳碰头地点
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) =
.1
|p2.x - p1.x| + |p2.y - p1.y|
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
Input:
1 - 0 - 0 - 0 - 1
| | | | |
0 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 1 - 0 - 0
Output: 6
Explanation: Given three people living at (0,0), (0,4), and (2,2):
The point (0,2) is an ideal meeting point, as the total travel distance
of 2+2+2=6 is minimal. So return 6.
朴素BFS(超时)- Time O(m^2 * n^2 ), Space O(mn)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public int minTotalDistance(int[][] grid) {
int minDistance = Integer.MAX_VALUE;
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
int distance = search(grid, row, col);
minDistance = Math.min(distance, minDistance);
}
}
return minDistance;
}
private int search(int[][] grid, int row, int col) {
Queue<Point> q = new LinkedList<>();
int m = grid.length;
int n = grid[0].length;
boolean[][] visited = new boolean[m][n];
q.add(new Point(row, col, 0));
int totalDistance = 0;
while (!q.isEmpty()) {
Point point = q.poll();
int r = point.row;
int c = point.col;
int d = point.distance;
if (r < 0 || c < 0 || r >= m || c >= n || visited[r][c]) {
continue;
}
if (grid[r][c] == 1) {
totalDistance += d;
}
visited[r][c] = true;
q.add(new Point(r + 1, c, d + 1));
q.add(new Point(r - 1, c, d + 1));
q.add(new Point(r, c + 1, d + 1));
q.add(new Point(r, c - 1, d + 1));
}
return totalDistance;
}
public class Point {
int row;
int col;
int distance;
public Point(int row, int col, int distance) {
this.row = row;
this.col = col;
this.distance = distance;
}
}
}
We could use the Selection algorithm to select the median in O(mn) time, but there is an easier way. Notice that we can collect both the row and column coordinates in sorted order.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public int minTotalDistance(int[][] grid) {
List<Integer> rows = calRows(grid);
List<Integer> cols = calCols(grid);
int row = rows.get(rows.size() / 2);
int col = cols.get(cols.size() / 2);
return minDistance1D(rows, row) + minDistance1D(cols, col);
}
private int minDistance1D(List<Integer> points, int origin) {
int distance = 0;
for (int point : points) {
distance += Math.abs(point - origin);
}
return distance;
}
private List<Integer> calRows(int[][] grid) {
List<Integer> rows = new ArrayList<>();
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
if (grid[row][col] == 1) {
rows.add(row);
}
}
}
return rows;
}
private List<Integer> calCols(int[][] grid) {
List<Integer> cols = new ArrayList<>();
for (int col = 0; col < grid[0].length; col++) {
for (int[] ints : grid) {
if (ints[col] == 1) {
cols.add(col);
}
}
}
return cols;
}
}