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 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 = 0y = 2, return 6.

 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
 6         int left = bsHor(image, 0, y, 0, m, true);
 7         int right = bsHor(image, y+1, n, 0, m, false);
 8         int top = bsVer(image, 0, x, left, right, true);
 9         int bottom = bsVer(image, x+1, m, left, right, false);
10
11         return (bottom - top) * (right - left);
12     }
13
14     private int bsHor(char[][] image, int i, int j, int top, int bottom, boolean leftSide) {
15         while (i != j) {
16             int k = top;
17             int mid = (j - i) / 2 + i;
18             while (k<bottom && image[k][mid] == ‘0‘) k++;
19             if (k < bottom == leftSide) {
20                 j = mid;
21             } else {
22                 i = mid + 1;
23             }
24         }
25         return i;
26     }
27
28     private int bsVer(char[][] image, int i, int j, int left, int right, boolean upSide) {
29         while (i != j) {
30             int k = left;
31             int mid = (j - i) / 2 + i;
32             while (k<right && image[mid][k] == ‘0‘) k++;
33             if (k < right == upSide) {
34                 j = mid;
35             } else {
36                 i = mid + 1;
37             }
38         }
39         return i;
40     }
41 }
时间: 2024-10-28 19:53:01

302. Smallest Rectangle Enclosing Black Pixels My Submissions QuestionEditorial Solution的相关文章

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

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

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

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

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 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

继续过Hard题目.0209

http://www.cnblogs.com/charlesblc/p/6372971.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

练练脑,继续过Hard题目

http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 LRU Ca

继续过Hard题目

接上一篇:http://www.cnblogs.com/charlesblc/p/6283064.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Difficulty Frequency   . 65 Valid Number     12.6% Hard    . 126 Word Ladder II     13.6% Hard    . 149 Max Points on a Line     15.6% Hard    . 146 L