标签:inline office ++ The 处理 前缀 post 二分 多个
1 or empty 0 (the number zero, one), find the place to build a post office, the distance that post office to all the house sum is smallest. Return the smallest distance. Return -1 if it is not possible.Example 1:
Input:[[0,1,0,0],[1,0,1,1],[0,1,0,0]] Output: 6 Explanation: Placing a post office at (1,1), the distance that post office to all the house sum is smallest.
Example 2:
Input:[[0,1,0],[1,0,1],[0,1,0]] Output: 4 Explanation: Placing a post office at (1,1), the distance that post office to all the house sum is smallest.
思路:
方法一:
方法二:
public class Solution {
/**
* @param grid: a 2D grid
* @return: An integer
*/
public int shortestDistance(int[][] grid) {
// Write your code here
int n = grid.length;
if (n == 0)
return -1;
int m = grid[0].length;
if (m == 0)
return -1;
List<Integer> sumx = new ArrayList<Integer>();
List<Integer> sumy = new ArrayList<Integer>();
List<Integer> x = new ArrayList<Integer>();
List<Integer> y = new ArrayList<Integer>();
int result = Integer.MAX_VALUE;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (grid[i][j] == 1) {
x.add(i);
y.add(j);
}
Collections.sort(x);
Collections.sort(y);
int total = x.size();
sumx.add(0);
sumy.add(0);
for (int i = 1; i <= total; ++i) {
sumx.add(sumx.get(i-1) + x.get(i-1));
sumy.add(sumy.get(i-1) + y.get(i-1));
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (grid[i][j] == 0) {
int cost_x = get_cost(x, sumx, i, total);
int cost_y = get_cost(y, sumy, j, total);
if (cost_x + cost_y < result)
result = cost_x + cost_y;
}
return result;
}
public int get_cost(List<Integer> x, List<Integer> sum, int pos, int n) {
if (n == 0)
return 0;
if (x.get(0) > pos)
return sum.get(n) - pos * n;
int l = 0, r = n - 1;
while (l + 1 < r) {
int mid = l + (r - l) / 2;
if (x.get(mid) <= pos)
l = mid;
else
r = mid - 1;
}
int index = 0;
if (x.get(r) <= pos)
index = r;
else
index = l;
return sum.get(n) - sum.get(index + 1) - pos * (n - index - 1) +
(index + 1) * pos - sum.get(index + 1);
}
}
标签:inline office ++ The 处理 前缀 post 二分 多个
原文地址:https://www.cnblogs.com/FLAGyuri/p/12078555.html