原题地址: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. 使用Java类库POI生成简易的Excel报表

    使用Java类库POI生成简易的Excel报表 1.需求 1.数据库生成报表需要转义其中字段的信息.比如 1,有效 2.无效等 2.日期格式的自数据需要转义其格式. 3.标题的格式和数据的格式需要分别 ...

  2. splay好板子

    找到一份比较好的板子,链接https://blog.csdn.net/crazy_ac/article/details/8034190 #include<cstdio> #include& ...

  3. springMVC3学习--ModelAndView对象(转)

    原文链接:springMVC3学习(二)--ModelAndView对象 当控制器处理完请求时,通常会将包含视图名称或视图对象以及一些模型属性的ModelAndView对象返回到DispatcherS ...

  4. Ubuntu 下安装LEMP环境 实战

    ---恢复内容开始--- 1.nginx的服务端的安装 打开命令行终端,在终端输入,sudo apt-get install nginx  回车即开始安装 kxlc-t@ubuntu:~$ sudo ...

  5. Web应用程序项目XXXX已配置为使用IIS。无法访问IIS 元数据库。您没有足够的特权访问计算机上的IIS

    错误图片:

  6. Python 可命名元祖

    import collections MytupleClass = collections.namedtuple('MytupleClass',['x','y','z']) obj = Mytuple ...

  7. pandas.cut使用总结

    用途 pandas.cut用来把一组数据分割成离散的区间.比如有一组年龄数据,可以使用pandas.cut将年龄数据分割成不同的年龄段并打上标签. 原型 pandas.cut(x, bins, rig ...

  8. hdu 1864 最大报销额【01背包】

    题目链接:https://vjudge.net/problem/HDU-1864 题目大意: 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求 ...

  9. HDU-2087-剪花布条 【KMP】(求模式串的匹配个数——与已匹配的字串不交)

    题目链接:https://vjudge.net/contest/220679#problem/C 剪花布条                                               ...

  10. python的time模块总结

    python的time模块与random模块 目录 time模块 time模块 三种时间表示 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) : 通常来说,时间戳表示的是 ...