[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 ...
随机推荐
- favicon
<link rel="icon" href="Images/wangyi.ico" type="text/plain" />
- opencv 中cvZero()的使用
cvZero(CvArr* ar); // 是让矩阵的值都为0,有初始化的作用, Eg: IplImage img=cvCreateImage(cvSize(640,480),IPL_DEPTH_ ...
- 许士彦:创业不走寻常路 APP最好时间已过
从用户体验服务设计公司eico design到拥有两千多万用户的独立微博客户端Weico,再到备受欢迎的影像APP(微可拍,Weico Gif),Weico团队走过了一条不寻常的创业之路. 作为一家新 ...
- 《Linux命令行与shell脚本编程大全》 第十四章 学习笔记
第十四章:呈现数据 理解输入与输出 标准文件描述符 文件描述符 缩写 描述 0 STDIN 标准输入 1 STDOUT 标准输出 2 STDERR 标准错误 1.STDIN 代表标准输入.对于终端界面 ...
- linux重新编译内核
一.linux内核 1.查看linux内核版本 uname -r 2.下载对应的linux内核 https://www.kernel.org/pub/linux/kernel/ 将内核文件夹解压到/u ...
- 关于虚拟机装kali-linux的联网问题
我用的是VMware Workstation11,近期装了一个kali-linux,想玩一下password破解.没想到装上之后网络连接显示的是活跃的却无法上网,我试过桥接等其他方式去联网,却依然无法 ...
- android 监听 USB 拔插广播消息
USBBroadcastReceiver.java package com.example.communication; import android.content.BroadcastReceive ...
- win7(32 bit) 环境下点击打印预览报错解决办法
如题,报错截图如下 : 解决办法如下: 1.关闭系统数据执行保护.具体操作: 在win7 系统命令行中执行如下命令: bcdedit.exe /set {current} nx Alw ...
- java 乱码详解_jsp中pageEncoding、charset=UTF -8"、request.setCharacterEncoding("UTF-8")
http://blog.csdn.net/qinysong/article/details/1179480 java 乱码详解__jsp中pageEncoding.charset=UTF -8&quo ...
- iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)
iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...