LeetCode OJ: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.
求出0,1矩阵中的最大矩形区域:
DP问题,可以使用数组记下当前行位置之前的所有1的个数,然后用一个三重循环来找以(i,j)为右下角的矩形的最大的面积,比较得到最大值。这样做复杂度还是比较高的。但是胜在简单,代码如下所示:
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if(matrix.size() == || matrix[].size() == )
return ;
int szRow = matrix.size();
int szCol = matrix[].size();
vector<vector<int>> dp(szRow, vector<int>(szCol, ));//表示当前行前面含有的1的个数
for(int i = ; i < szRow; ++i){ //第一列的每一行的第一个元素
dp[i][] = matrix[i][] == '' ? : ;
}
for(int i = ; i < szRow; ++i){ //后面每列的每行的每个元素
for(int j = ; j < szCol; ++j){
dp[i][j] = matrix[i][j] == '' ? dp[i][j-]+ : ;
}
}
int maxVal = ;
for(int i = ; i < szRow; ++i){
for(int j = ; j < szCol; ++j){
int width = INT_MAX;
for(int k = i; k >= ; --k){
if(dp[k][j] == )
break;
width = min(width, dp[k][j]);//求出每次正方形的宽度
maxVal = max(maxVal, width * (i - k + ));//宽度乘以高度
}
}
}
return maxVal;
}
};
先马一下别人写的方法,感觉写的挺好的,有时间在来写一下
LeetCode OJ:Maximal Rectangle(最大矩形)的更多相关文章
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- [LintCode] Maximal Rectangle 最大矩形
Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 【leetcode】Maximal Rectangle (hard)★
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- LeetCode OJ 223.Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- [LeetCode] Construct the Rectangle 构建矩形
For a web developer, it is very important to know how to design a web page's size. So, given a speci ...
随机推荐
- cocos2d-x项目中如何避免增加一个cpp就必须在工程android.mk文件去添加引用
LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../Classes/AppDelegate.cpp \ ../../Classes/HelloWorl ...
- 如何在idea中设置 jsp 内容修改以后,立即生效而不用重新启动服务?
点击 run---->edit configuration--->
- 转载:逻辑回归的python实现
转载自:http://blog.csdn.net/zouxy09/article/details/20319673 一.逻辑回归(LogisticRegression) Logistic regres ...
- linux 用到的命令
1. cd / 直接到服务器根目录 2. pwd 查看 当前所在目录 3. ll 查看当前文件下文件列表 4.ls 查看当前文件下文件,横向排列 5.ps -ef | gre ...
- Mysql 数据类型及选择原则
MySQL中的数据类型大的方面来分,可以分为:日期和时间.数值,以及字符串.下面就分开来进行总结. 数据库类型的选择对数据库的性能影响很大 1 . 数据类型会影响存储空间的开销 2 . 数据类型会影响 ...
- Python- discover()方法与执行顺序补充
可以根据不同的功能创建不同的测试文件,甚至是不同的测试目录,测试文件中还可以将不同的小功能划分为不同的测试类,在类下编写测试用例,让整体结构更加清晰 但通过addTest()添加.删除测试用例就变得非 ...
- [转]MySQL查看数据库相关信息
原文链接:MySQL查看数据库相关信息 使用MySQL时,需要了解当前数据库的情况,例如当前的数据库大小.字符集.用户等等.下面总结了一些查看数据库相关信息的命令 1:查看显示所有数据库 mysql& ...
- handle 和module
<httpHandlers> <add verb="*" path="*" type="ClassLibrary831.TestHa ...
- 有道云笔记配合MPic+七牛云 自制MarkDown文档图床(适用Typora)
注:从有道云笔记v6.5开始,有道云笔记会员可以使用MarkDown有道自带的图床.(但是非会员可以采用下面的七牛云图床+MarkDown方法) 0x00 前言 一直用有道云笔记,粘贴图片,做笔记没问 ...
- CSS Float(浮动)
CSS Float(浮动) 一.CSS Float(浮动) CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列. Float(浮动),往往是用于图像,但它在布局时一样非常 ...