302. Smallest Rectangle Enclosing Black Pixels (H)

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.

Example:

Input:
[
"0010",
"0110",
"0100"
]
and x = 0, y = 2

Output: 6


我的思路:DFS,但是其实和brute force区别不大

答案的第三种:binary search

class Solution {
public:
    int minArea(vector>& image, int x, int y) {
        int leftmost=0, rightmost=0, topmost=0, bottommost=0;
        
        int left=0, right=y, mid, row;
        while (left < right) {
            mid = (left + right)/2;
            for (row=0; row

Runtime: 84 ms, faster than 65.52% of C++ online submissions for Smallest Rectangle Enclosing Black Pixels.
Memory Usage: 16.4 MB, less than 98.28% of C++ online submissions for Smallest Rectangle Enclosing Black Pixels.

你可能感兴趣的:(302. Smallest Rectangle Enclosing Black Pixels (H))