- Build Post Office
Given a 2D grid, each cell is either an house 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
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.
Notice
You can pass through house and empty.
You only build post office on an empty.
The distance between house and the post office is Manhattan distance
解法1:
我的方法是先预处理,得到left2RightCount, right2LeftCount, left2RightDist, right2LeftDist这4个二维数组。注意我们不需要up2down和down2up这样的数组,因为根据Manhattan距离,先左后下和先下后左是一样的。
然后我们遍历整个grid,对每个0元素,固定列,遍历行(0…nRow-1),对每行的left2RightDist和righ2LeftDist加起来,然后再根据每行到i的距离乘以left2RightCount和right2LeftCount的和(注意如果[k][j]是1的话,和要减1)。
该法还是稍慢,因为复杂度是O(m^2*n)。
代码如下:
class Solution {
public:
/**
* @param grid: a 2D grid
* @return: An integer
*/
int shortestDistance(vector<vector<int>> &grid) {
int nRow = grid.size();
int nCol = grid[0].size();
if (nRow == 0 || nCol == 0) return 0;
left2RightDist.resize(nRow, vector<int>(nCol, 0));
right2LeftDist = left2RightDist;
left2RightCount = left2RightDist;
right2LeftCount = left2RightDist;
//preprocessing
for (int i = 0; i < nRow; ++i) {
for (int j = 0; j < nCol; ++j) {
int tmpCount = (j == 0) ? 0 : left2RightCount[i][j - 1];
left2RightCount[i][j] = (grid[i][j] == 1) ? tmpCount + 1 : tmpCount;
int tmpDist = (j == 0) ? 0 : left2RightDist[i][j - 1];
left2RightDist[i][j] = tmpDist + left2RightCount[i][j] -
((grid[i][j] == 1) ? 1 : 0);
}
for (int j = nCol - 1; j >= 0; --j) {
int tmpCount = (j == nCol - 1) ? 0 : right2LeftCount[i][j + 1];
right2LeftCount[i][j] = (grid[i][j] == 1) ? tmpCount + 1 : tmpCount;
int tmpDist = (j == nCol - 1) ? 0 : right2LeftDist[i][j + 1];
right2LeftDist[i][j] = tmpDist + right2LeftCount[i][j] -
((grid[i][j] == 1) ? 1 : 0);
}
}
int minTotalDis = INT_MAX;
for (int i = 0; i < nRow; ++i) {
for (int j = 0; j < nCol; ++j) {
if (grid[i][j] == 0) {
int distSum = 0, countSum = 0;
for (int k = 0; k < nRow; ++k) {
countSum = left2RightCount[k][j] +
right2LeftCount[k][j] -
((grid[k][j] == 1) ? 1 : 0);
int tempSum = left2RightDist[k][j] +
right2LeftDist[k][j];
distSum += tempSum + countSum * abs(i - k);
}
minTotalDis = min(minTotalDis, distSum);
}
}
}
return minTotalDis;
}
private:
int nRow, nCol;
vector<vector<int>> left2RightDist, right2LeftDist;
vector<vector<int>> left2RightCount, right2LeftCount;
};
二刷:进行预处理。时间复杂度是O(mn)。注意:如果用BFS,时间复杂度是O(m ^ 2 * n ^ 2)。
class Solution {
public:
/**
* @param grid: a 2D grid
* @return: An integer
*/
int shortestDistance(vector<vector<int>> &grid) {
int nRow = grid.size(), nCol = grid[0].size();
vector<int> rowCnt(nRow, 0), rowSum(nRow, 0);
vector<int> colCnt(nCol, 0), colSum(nCol, 0);
int res = INT_MAX;
for (int i = 0; i < nRow; i++) {
for (int j = 0; j < nCol; j++) {
if (grid[i][j] == 0) continue;
rowCnt[i]++;
rowSum[i] += i;
colCnt[j]++;
colSum[j] += j;
}
}
genPresum(rowCnt);
genPresum(rowSum);
genPresum(colCnt);
genPresum(colSum);
for (int i = 0; i < nRow; i++) {
for (int j = 0; j < nCol; j++) {
if (grid[i][j] == 1) continue;
int sum = rowCnt[i] * i - rowSum[i] + (rowSum[nRow - 1] - rowSum[i]) - (rowCnt[nRow - 1] - rowCnt[i]) * i +
colCnt[j] * j - colSum[j] + (colSum[nCol - 1] - colSum[j]) - (colCnt[nCol - 1] - colCnt[j]) * j;
res = min(res, sum);
}
}
return res;
}
private:
void genPresum(vector<int> &nums) {
for (int i = 1; i < nums.size(); i++) {
nums[i] += nums[i - 1];
}
}
};