Leetcode: Smallest Rectangle Enclosing Black Pixels

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.

Imagine we project the 2D array to the bottom axis with the rule "if a column has any black pixel it‘s projection is black otherwise white". The projected 1D array is "0110"

Theorem

If there are only one black pixel region, then in a projected 1D array all the black pixels are connected.

This means we can do a binary search in each half to find the boundaries, if we know one black pixel‘s position. And we do know that.

To find the left boundary, do the binary search in the [0, y) range and find the first column vector who has any black pixel.

To determine if a column vector has a black pixel is O(m) so the search in total is O(m log n)

We can do the same for the other boundaries. The area is then calculated by the boundaries. Thus the algorithm runs in O(m log n + n log m)

 1 public class Solution {
 2     public int minArea(char[][] image, int x, int y) {
 3         int m = image.length;
 4         int n = image[0].length;
 5         int left = searchLeft(image, y);
 6         int right = searchRight(image, y);
 7         int top = searchTop(image, x);
 8         int bottom = searchBottom(image, x);
 9         return (right-left+1)*(bottom-top+1);
10     }
11
12     public int searchLeft(char[][] image, int right) {
13         int l = 0, r = right;
14         while (l <= r) {
15             int m = (l+r)/2;
16             if (!findBlack(image, m, false)) l = m+1;
17             else r = m-1;
18         }
19         return l;
20     }
21
22     public int searchRight(char[][] image, int left) {
23         int l = left, r = image[0].length-1;
24         while (l <= r) {
25             int m = (l+r)/2;
26             if (!findBlack(image, m, false)) r = m-1;
27             else l = m+1;
28         }
29         return r;
30     }
31
32     public int searchTop(char[][] image, int bottom) {
33         int l = 0, r = bottom;
34         while (l <= r) {
35             int m = (l+r)/2;
36             if (!findBlack(image, m, true)) l = m+1;
37             else r = m-1;
38         }
39         return l;
40     }
41
42     public int searchBottom(char[][] image, int top) {
43         int l=top, r=image.length-1;
44         while (l <= r) {
45             int m = (l+r)/2;
46             if (!findBlack(image, m, true)) r = m-1;
47             else l = m+1;
48         }
49         return r;
50     }
51
52     public boolean findBlack(char[][] image, int cur, boolean row) {
53         int m = image.length;
54         int n = image[0].length;
55         if (row) {
56             for (int i=0; i<n; i++) {
57                 if (image[cur][i] == ‘1‘)
58                     return true;
59             }
60             return false;
61         }
62         else {
63             for (int j=0; j<m; j++) {
64                 if (image[j][cur] == ‘1‘)
65                     return true;
66             }
67             return false;
68         }
69     }
70 }
 
时间: 2024-10-10 14:13:30

Leetcode: Smallest Rectangle Enclosing Black Pixels的相关文章

LeetCode 302. Smallest Rectangle Enclosing Black Pixels

DFS 最一开始想到的就是dfs,从题目给的点开始dfs搜索. 时间复杂度为O(mn) class Solution { public: int i_min=INT_MAX, i_max=INT_MIN; int j_min=INT_MAX, j_max=INT_MIN; vector<vector<int>> dirs={{1,0},{-1,0},{0,1},{0,-1}}; int minArea(vector<vector<char>>& im

302. Smallest Rectangle Enclosing Black Pixels

题目: 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

302. Smallest Rectangle Enclosing Black Pixels My Submissions QuestionEditorial Solution

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

LC 302. Smallest Rectangle Enclosing Black Pixels【lock, 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

Smallest Rectangle Enclosing Black Pixels

Note: 约等于O(nlogn) public class Solution { /** * @param image a binary matrix with '0' and '1' * @param x, y the location of one of the black pixels * @return an integer */ public int minArea(char[][] image, int x, int y) { // Write your code here if(

LeetCode:Rectangle Area - 矩形交叉部分的面积

1.题目名称 Rectangle Area(矩形交叉部分的面积) 2.题目地址 https://leetcode.com/problems/rectangle-area/ 3.题目内容 英文:Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown

(每日算法)Leetcode -- Largest Rectangle in Histogram(最大实心矩形)

思路:如果时间复杂度要求是O(n 2 )的话,解法比较多也比较好理解.比如可以遍历,对于当前 i 位置上的立柱,计算出以这个i 立柱结尾的最大矩形,然后求出总的最大矩形. Given  n  non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above

LeetCode: Maximal Rectangle

LeetCode: Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 地址:https://oj.leetcode.com/problems/maximal-rectangle/ 算法:要解决这道题,得利用Largest Rectangle in Histogram这道题的解法

LeetCode: Maximal Rectangle [085]

[题目] Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. [题意] 给定一个由0和1填充的二维矩阵,找一个全是1的最大矩形 [思路] 扫描二维矩阵,凡是扫到值为1的块时候,以当前块为矩形的左上角区块拓展,找最大矩阵. 先找出以每个"1"区块为左上角区块的最大矩形,然后求出最大全局的最大矩形. 以下图为