《Leetcode of December》85.最大矩形

class Solution:
    def maximalRectangle(self, matrix: List[List[str]]) -> int:
        if not matrix or not matrix[0]:
            return 0
        
        #计算有几个连续的1
        def getWidth(num):
            count = 0
            while num>0:
                num&=num<<1
                count+=1
            return count

        nums=[int(''.join(row),base=2) for row in matrix]
        ans,N = 0,len(nums)
        for i in range(N):
            temp = nums[i]
            for j in range(i,N):
                temp = temp&nums[j]
                width = getWidth(temp)
                hight = j-i+1
                ans = max(ans,width*hight)
        return ans

总结:和最大正方形如出一辙,还是需要注意怎么计算二进制中有几个连续的1。 

猜你喜欢

转载自blog.csdn.net/weixin_37724529/article/details/111769703