85. Maximal Rectangle

这一题的算法就是按照坐标搜索,假设起始坐标是i, j ,按行搜索,c和r表示相对于i,j这个坐标的相对坐标,搜索到这一行最远为1的位置,然后继续搜索该坐标下面一行,算res = max(res, (c+1)*(r+1)),
代码如下:

class Solution(object):
    def maximalRectangle(self, matrix):
        """
        :type matrix: List[List[str]]
        :rtype: int
        """
        res = 0
        if len(matrix) == 0:
            return 0
        m = len(matrix)
        n = len(matrix[0])
        res = 0
        for i in range(m):
            for j in range(n):
                res = max(res, self.max_ij(matrix, i, j, m, n))
        return res
    
    def max_ij(self, matrix, i, j, m, n):
        if matrix[i][j] == "0":
            return 0
        else:
            c = 0
            r = 0
            res = 0
            c_pre = n - 1
            while j+c < n and i+r < m and matrix[i+r][j+c] == "1":
                while j+c < n and i + r < m  and c <= c_pre and matrix[i+r][j+c] == "1" :
                    c += 1
                c -= 1
                #print c
                c = min(c, c_pre)
                res = max(res, (c+1)*(r+1))
                #print res
                #print i + r
                c_pre = c
                r += 1
                c = 0
            return res
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 6,253评论 0 2
  • 回溯算法 回溯法:也称为试探法,它并不考虑问题规模的大小,而是从问题的最明显的最小规模开始逐步求解出可能的答案,并...
    fredal阅读 14,703评论 0 89
  • Given a 2D binary matrix filled with 0's and 1's, find th...
    Jeanz阅读 2,629评论 0 0
  • 原题 给你一个二维矩阵,权值为False和True,找到一个最大的矩形,使得里面的值全部为True,输出它的面积 ...
    Jason_Yuan阅读 8,060评论 0 1
  • 你依旧是我的软肋,却已经不再是我的盔甲。 我在新年的第一天把你所有的联系方式都删了,狠心不在联系你。从那时候开始,...
    飞驰的婷婷阅读 1,544评论 0 0

友情链接更多精彩内容