LeetCode 题解(284) : 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.

题解:

用了两种做法BFS (332ms) 和Binary Search (60ms).

BFS:

class Solution(object):
    def minArea(self, image, x, y):
        """
        :type image: List[List[str]]
        :type x: int
        :type y: int
        :rtype: int
        """
        if len(image) == 0:
            return 0
        left, right, top, bot = len(image[0]) - 1, 0, len(image) - 1, 0
        visited = [[False for i in range(len(image[0]))] for j in range(len(image))]
        q = []
        q.append((x, y))
        while len(q) != 0:
            cur = q.pop()
            m, n = cur[0], cur[1]
            visited[m][n] = True
            if n < left:
                left = n
            if n > right:
                right = n
            if m < top:
                top = m
            if m > bot:
                bot = m
            if m - 1 >= 0 and image[m-1][n] == "1" and not visited[m-1][n]:
                q.append((m-1, n))
            if m + 1 < len(image) and image[m+1][n] == "1" and not visited[m+1][n]:
                q.append((m+1, n))
            if n - 1 >= 0 and image[m][n-1] == "1" and not visited[m][n-1]:
                q.append((m, n-1))
            if n + 1 < len(image[0]) and image[m][n+1] == "1" and not visited[m][n+1]:
                q.append((m, n+1))
        return (right - left + 1) * (bot - top + 1)

Binary Search:

class Solution(object):
    def minArea(self, image, x, y):
        """
        :type image: List[List[str]]
        :type x: int
        :type y: int
        :rtype: int
        """
        if len(image) == 0:
            return 0
        #left, right, top, bot = len(image[0]) - 1, 0, len(image) - 1, 0
        top = self.searchV(image, 0, x, True)
        bot = self.searchV(image, x + 1, len(image), False)
        left = self.searchH(image, 0, y, top, bot, True)
        right = self.searchH(image, y + 1, len(image[0]), top, bot, False)
        return (right - left) * (bot - top)

    def searchV(self, image, low, high, opt):
        while low < high:
            mid = (low + high) / 2
            if ('1' in image[mid]) == opt:
                high = mid
            else:
                low = mid + 1
        return low

    def searchH(self, image, low, high, top, bot, opt):
        while low < high:
            mid = (low + high) / 2
            found = False
            for i in range(top, bot):
                if (image[i][mid] == '1'):
                    if opt:
                        high = mid
                    else:
                        low = mid + 1
                    found = True
                    break
            if not found:
                if opt:
                    low = mid + 1
                else:
                    high = mid
        return low

你可能感兴趣的:(LeetCode,Algorithm,面试题)