[leetcode]Maximal Rectangle @ Python
原题地址: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的更多相关文章
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- LeetCode: Maximal Rectangle 解题报告
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Maximal Rectangle(good)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 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 ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
随机推荐
- silverlight处理gif格式图片
1.在工具箱中添加dll文件 2.工具栏中将显示此控件,填写xaml文件 <UserControl xmlns="http://schemas.microsoft.com/winfx/ ...
- HTML5——Data Url生成
HTML5——Data Url生成 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- ASP.NET Identity详解
Asp.Net Identiy是ASP.NET身份验证机制. 如何构建安全的Web应用? 我们先来思考一个问题:如何构建安全的WEB应用? 一直以来,这都是比较热门的话题.不幸的是,目前还没有一种 ...
- C#学习网站收集
1. 大名鼎鼎的CodeGuru 号称代码领头羊 非常著名的关于程序开发的网站,大量的资料.强烈推荐 http://www.codeguru.com/ - 外文 2. Developer.com: A ...
- Webservice返回json数据格式
问题: 我将结果内容用字符串拼接成Json数据并返回的时候,会在结果前面添加xml头部,结果如下. <span ><string xmlns="http://tempuri ...
- Jenkins Pipline语法
引用自:http://baijiahao.baidu.com/s?id=1582812185263227836&wfr=spider&for=pc 引用自:https://www.cn ...
- BZOJ1898 [Zjoi2005]Swamp 沼泽鳄鱼 矩阵
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1898 题意概括 有一个无向图. 其中,有许多条鱼在以循环的规律出现,比如循环在1,2,3这些点出现 ...
- 【Java】 剑指offer(10) 旋转数组的最小数字
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. ...
- 052 自动将每日的日志增量导入到hive中
一:大纲介绍 1.导入方式 load data local inpath 'local_file_path' into table tbname partition (date='',hour='') ...
- unbuntu 18.04 LTS 版 安装Samba服务器
首先需要注意的是,此时服务器的版本是乌班图18.04 LTS Server ,安装配置Samba服务器 . 首先来对当前的软件进行更新. 1 sudo apt-get upgrade 2 sudo ...