[LeetCode]题解(python):085-Maximal Rectangle
题目来源:
https://leetcode.com/problems/maximal-rectangle/
题意分析:
给定一个二维的二进制矩阵,也就是只包括0 和 1的,找出只包括1的最大的矩阵的面积。
题目思路:
这道题目可以用84题的解决方法去解决。如果选定了一行为起点,这一行向上或者向下从这行开始连续1的个数是bar的高度,那么就可以求出以这一行起得到的最大的矩阵面积。遍历所有的行,得到最大的结果为这题的最后结果。
代码(python):
class Solution(object):
def maximalRectangle(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
m = len(matrix)
if m == 0:
return 0
n = len(matrix[0])
def solve(s):
mans = 0
ans,ansindex,i = [],[],0
while i < len(s):
if len(ans) == 0 or s[i] >ans[-1]:
ans.append(s[i]);ansindex.append(i)
elif s[i] < ans[-1]:
lastindex = 0
while len(ans) > 0 and ans[-1] > s[i]:
lastindex = ansindex.pop()
mans = max(mans,ans.pop() * (i - lastindex))
ans.append(s[i]);ansindex.append(lastindex)
i += 1
while len(ans) != 0:
mans = max(mans,ans.pop() * (len(s) - ansindex.pop()))
return mans
s = [0 for i in range(n)]
ans = 0
for i in range(m):
for j in range(n):
if matrix[i][j] == '':
s[j] += 1
else:
s[j] = 0
ans = max(ans,solve(s))
return ans
[LeetCode]题解(python):085-Maximal Rectangle的更多相关文章
- 【LeetCode】085. Maximal Rectangle
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's ...
- Java for LeetCode 085 Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- LeetCode 笔记系列 18 Maximal Rectangle [学以致用]
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- 085 Maximal Rectangle 最大矩形
给定一个填充了 0 和 1 的二进制矩阵,找到最大的只包含 1 的矩形并返回其面积.例如,给出以下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 6 详见:http ...
- LeetCode题解:(221) Maximal Square
题目说明 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- LeetCode: Maximal Rectangle 解题报告
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...
随机推荐
- 黑马程序员_<<String类>>
--------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1. String类 1.概述 String ...
- vs2010调试快捷键
VS2010单步调试 1.设置断点 F9设置或者取消断点,如果当前行未设置断点,则F9可以再当前行设置断点,如果已经设置,则为去除断点 2.单步调试 F10不进入函数单步,F11进入函数单步 , ...
- JavaScript之arguments.callee
arguments.callee 在哪一个函数中运行,它就代表哪个函数. 一般用在匿名函数中. 在匿名函数中有时会需要自己调用自己,但是由于是匿名函数,没有名子,无名可调. 这时就可以用argumen ...
- 试用cmd markdown
欢迎使用 Cmd Markdown 编辑阅读器 我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd Markdown 是我们给出的答案 -- 我们为记录思想 ...
- 基于Jquery的Ajax分页,只有上一页和下一页
最近项目中用到ajax分页 在网上找到一个非常好用的分页插件jquery-pagination-ajax,以下是链接 http://www.zhangxinxu.com/wordpress/2010/ ...
- Oracle中的日期和字符串互相转换
转载出处:http://blog.sina.com.cn/s/blog_44a005380100k6rv.html TO_DATE格式(以时间:2007-11-02 13:45:25为例) ...
- C++学习之嵌套类和局部类
C++学习之嵌套类和局部类 局部类 在一个函数体内定义的类称为局部类. 局部类中只能使用它的外围作用域中的对象和函数进行联系,因为外围作用域中的变量与该局部类的对象无关.在定义局部类时需要注意:局部类 ...
- hexo博客部署到github无法上传的问题
博客生成之后,按照网上别人的教程,讲项目部署到github上,修改_config.yaml中的deploy部分如下所示: deploy: type: git repository: https://g ...
- 在sqlite中使用索引
出处: 网络 1)Sqlite不支持聚集索引,android默认需要一个_id字段,这保证了你插入的数据会按“_id”的整数顺序插入,这个integer类型的主键就会扮演和聚集索引一样的角色.所以不要 ...
- Changing the Auto-Logout Timeout in SSH
SSH: We can set a timeout interval for ssh client who are idle or in inactive state. As soon as the ...