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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值