原题地址:https://oj.leetcode.com/problems/maximal-rectangle/

题意:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

解题思路:找出矩阵中最大的矩形,矩形中只包含1。这道题需要利用上一道题(Largest Rectangle in Histogram)的结论。比如对于以下矩阵。

        0 0 0 0

        0 0 1 0

        0 1 1 0

        1 0 1 1

     对于这个矩阵,对于每一行,我们按照上一道题的算法求解一遍,最后得出的就是最大的矩阵。

代码:

class Solution:
# @param matrix, a list of lists of 1 length string
# @return an integer
def largestRectangleArea(self, height):
stack=[]; i=0; area=0
while i<len(height):
if stack==[] or height[i]>height[stack[len(stack)-1]]:
stack.append(i)
else:
curr=stack.pop()
width=i if stack==[] else i-stack[len(stack)-1]-1
area=max(area,width*height[curr])
i-=1
i+=1
while stack!=[]:
curr=stack.pop()
width=i if stack==[] else len(height)-stack[len(stack)-1]-1
area=max(area,width*height[curr])
return area def maximalRectangle(self, matrix):
if matrix==[]: return 0
a=[0 for i in range(len(matrix[0]))]; maxArea=0
for i in range(len(matrix)):
for j in range(len(matrix[i])):
a[j]=a[j]+1 if matrix[i][j]=='' else 0 maxArea=max(maxArea, self.largestRectangleArea(a)) return maxArea

[leetcode]Maximal Rectangle @ Python的更多相关文章

  1. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  2. LeetCode: Maximal Rectangle 解题报告

    Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...

  3. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  4. [LeetCode] Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  5. [LeetCode] Maximal Rectangle(good)

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  6. leetcode -- Maximal Rectangle TODO O(N)

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  7. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  8. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  9. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

随机推荐

  1. linux + docker + selenium grid 实现分布式执行selenium脚本

    Selenium Grid 有两个概念 hub :主节点,你可以看作 "北京总公司的测试经理". node:分支节点,你可以看作 "北京总公司的测试小兵A" 和 ...

  2. Ext.js入门(二)

        ExtJs OOP基础 一:ExtJs中的面向对象 1.ExtJs中命名空间的定义        Ext中的命名空间类似于C#中的namespace和java中的包,用来对工程中的类进行更好的 ...

  3. 开始写博客,学习Linq(5)

    开始写代码了,我会把自己的代码粘贴在这里,好不容易可以实践了,可是不能偷懒的. string[] words = { "hello", "wonderful", ...

  4. zjoi 小星星

    题解: dp很容易想 f[i][j][s]表示匹配到了i点 对应点为j点,状态为s 那么这样的时间复杂度为(3^n*n^2) 然后会发现这其实可以转化为可以重复利用元素的子集卷积 http://www ...

  5. c# 服务安装后自动启动

    switch (rs)            {                case 1:                                       var path = @&q ...

  6. Adobe Acrobat Pro DC破解

    下载amtemu 点击 在AMT Emulator界面,下拉选择Adobe Acrobat DC; 然后点击右下角Install安装破解补丁. 点击Install后在弹出窗口中手动找到并选择软件安装目 ...

  7. 【Java】 剑指offer(3) 二维数组中的查找

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上 ...

  8. 【Java】 剑指offer(35) 复杂链表的复制

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请实现函数ComplexListNode* Clone(Compl ...

  9. hdu-1754 I Hate It【线段树】(求区间最大值)

    <题目链接> I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  10. Python常用模块--string

    该模块提供3个常用的功能: * 提供常用的字符串常量(感觉用途不大) * 提供字符串替换功能,主要用途是上下文的国际化(通过str可以实现,不介绍,感兴趣的自己看官网) * 提供字符串的格式化功能(感 ...