LeetCode 题解(284) : Smallest Rectangle Enclosing Black Pixels

本文介绍了一种寻找二进制矩阵中所有黑色像素所包围最小矩形面积的算法。提供了两种实现方法:广度优先搜索(BFS)和二分查找(Binary Search),并详细解释了这两种方法的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值