[LeetCode302]Smallest Rectangle Enclosing Black Pixels
来源:互联网 发布:威海近年来人口数据 编辑:程序博客网 时间:2024/04/26 05:31
Hard ..An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.For example, given the following image: [ "0010", "0110", "0100" ]and x = 0, y = 2,Return 6.Hide Company Tags GoogleHide Tags Binary Search
如果选中的点是白色,那么最小的面积就是选中的点作为一个边界,加上整个黑色面积。若选中的是黑色,那么最小面积就是整个黑色面积(因为它们连在一起)。
class Solution {public: int minArea(vector<vector<char>>& image, int x, int y) { int m = image.size(), n = image[0].size(); int top = searchRow(image, 0, x, 0, n, true); int bottom = searchRow(image, x+1, m, 0, n, false); int left = searchCols(image, 0, y, top, bottom, true); int right = searchCols(image, y+1, n, top, bottom, false); return (bottom - top) * (right - left); } int searchRow(vector<vector<char>>& image, int start, int end, int lo, int hi, bool opt){ while(start != end){ int k = lo, mid = start + (end-start)/2; while(k < hi && image[mid][k] == '0') ++k; if(k<hi == opt) end = mid;// k<hi && opt || k>hi && !opt else start = mid + 1; } return start; } int searchCols(vector<vector<char>>& image, int start, int end, int lo, int hi, bool opt){ while(start != end){ int k = lo, mid = start + (end - start)/2; while(k < hi && image[k][mid] == '0') ++k; if(k < hi == opt) end = mid; else start = mid + 1; } return start; }};
0 0
- [LeetCode302]Smallest Rectangle Enclosing Black Pixels
- Smallest Rectangle Enclosing Black Pixels
- leetcode 301: Smallest Rectangle Enclosing Black Pixels
- [leetcode-302]Smallest Rectangle Enclosing Black Pixels
- *LeetCode-Smallest Rectangle Enclosing Black Pixels
- Leetcode: Smallest Rectangle Enclosing Black Pixels
- 302. Smallest Rectangle Enclosing Black Pixels [leetcode]
- LeetCode 题解(284) : Smallest Rectangle Enclosing Black Pixels
- [leetcode] 302. Smallest Rectangle Enclosing Black Pixels 解题报告
- [Leetcode] 302. Smallest Rectangle Enclosing Black Pixels 解题报告
- LeetCode 302. Smallest Rectangle Enclosing Black Pixels(最小面积矩形)
- (beginer) 旋转卡壳 UVA 12307 - Smallest Enclosing Rectangle
- uva 12307 - Smallest Enclosing Rectangle(旋转卡壳)
- UVa 12307 Smallest Enclosing Rectangle(旋转卡壳+最小覆盖矩形)
- Smallest Bounding Rectangle - uva10173
- hihocoder #1442 : Smallest Rectangle
- UVa 10173 - Smallest Bounding Rectangle
- uva 10173 Smallest Bounding Rectangle
- [LeetCode294]Flip Game II
- [LeetCode296]Best Meeting Point
- 题解: HDU 1254 :推箱子 (BFS)
- [LeetCode298]Binary Tree Longest Consecutive Sequence
- JPanel
- [LeetCode302]Smallest Rectangle Enclosing Black Pixels
- 模板:HDU 1242: Rescue: (BFS DFS)
- [LeetCode305]Number of Islands II
- 再谈敏捷开发的好处及敏捷外包的前景
- shell脚本的各种判断
- [LeetCode308]Range Sum Query 2D - Mutable
- 深入理解Java的接口和抽象类
- [LeetCode311]Sparse Matrix Multiplication
- 敏捷开发之Scrum扫盲篇