[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 ...
随机推荐
- NSFileHandle编写json数据格式
代码如下: + (void)writeToFile:(NSDictionary *)params filePath:(NSString *)path { NSData *jsonData = [sel ...
- Minimum Inversion Number(归并排序)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 轻轻谈一下seaJs——模块化开发的利器
"仅做一件事,做好一件事." 这个应该就是seaJs的精髓了. 我在自己的一些项目中使用过seaJs.对其算是了解一二.如今就班门弄斧.轻轻地谈一下. 首先上一段度娘的话: &qu ...
- selector是在文件夹drawable中进行定义的xml文件。
获取Drawable对象: Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable ...
- Ubuntu - Dconf 注册表键值修改参考表
gsettings reset org.gnome.desktop.wm.preferences theme默认gnomegsettings set org.gnome.desktop.interfa ...
- matlab GUI之常用对话框(三)-- dialog \ errordlg \ warndlg \ helpdlg \ msgbox \questdlg
常用的对话框(三) 1.普通对话框 dialog 调用格式: h=dialog('PropertyName','PropertyValue'......) %普通对话框 h=dialog( ]); ...
- Struts2相关注意点
今天开始学习Struts2,有一个小栗子用到了DMI动态方法调用,使用DMI可以减少action的数量简化程序,不用在struts.xml的action中定义method属性.刚开始怎么也不能使用DM ...
- nginx install
./configure --prefix=/home/allen.mh/local/nginx --with-http_ssl_module --with-http_sub_module --with ...
- Android 汉字转拼音之JNI篇
package com.tool.hz2py; import android.os.Bundle; import android.app.Activity; import android.view.M ...
- 安卓里面JSON处理和JAVA SE里面的JSON包
今天编译安卓项目遇到这个问题 com.android.dex.DexException: Multiple dex files define的解决办法 大致意思就是引用了 相同的包 在JAVA SE里 ...