首先对于lowerbound和upperbound,可以参考Search for Range,如果我们搜索下边界和上边界的后一个数的话,是可以共用代码的。那么对于行和列的共用代码,其实并不难,参考下面的代码很容易懂。时间复杂度O(m * log n + n * log m),常数空间,代码如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int minArea(vector<vector<char>>& image, int x, int y) { | |
int m = image.size(), n = m? image[0].size(): 0; | |
int top = search(image, 0, x, n, true, true); | |
int bottom = search(image, x + 1, m, n, true, false); | |
int left = search(image, 0, y, m, false, true); | |
int right = search(image, y + 1, n, m, false, false); | |
return (bottom - top) * (right - left); | |
} | |
private: | |
//for lowerbound, reurn lowerbound; for upper bound, return upperbould + 1 | |
int search(vector<vector<char>>& image, int lo, int hi, int end, bool isRow, bool isLowerBound) | |
{ | |
while(lo < hi) | |
{ | |
int mid = lo + (hi - lo) / 2; | |
bool hasBlack = false; | |
for(int j = 0; j < end; ++j) | |
{ | |
if(isBlack(image, mid, j, isRow)) | |
{ | |
hasBlack = true; | |
break; | |
} | |
} | |
if(isLowerBound) | |
{ | |
if(hasBlack) | |
hi = mid; | |
else | |
lo = mid + 1; | |
} | |
else | |
{ | |
if(hasBlack) | |
lo = mid + 1; | |
else | |
hi = mid; | |
} | |
} | |
return lo; | |
} | |
bool isBlack(vector<vector<char>>& image, int i, int j, bool isRow) | |
{ | |
return isRow? image[i][j] == '1': image[j][i] == '1'; | |
} | |
}; |
No comments:
Post a Comment