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 ...
随机推荐
- springer论文模板参考文献的顺序问题
latex环境 MikTex 2.9 + TeXstudio 2.12.8 (+ Mendeley) 问题 springer提供的latex模板 中最后的参考文献是按照字母顺序排列的.我想要弄成按照文 ...
- NC开发笔记指导
修改端口 InvocationInfoProxy.getInstance().get().getUserCode(); 前台 Nchome F:\JAVA\Projects\ERPEHROA\ufid ...
- nginx rewrite规则last与break的区别
概要:break和last都能阻止继续执行后面的rewrite指令,last如果在location下的话,对于重写后的URI会重新匹配location,而break不会重新匹配location. 区别 ...
- javascript 基本数据类型、引用数据类型
阅读目录 数据类型 两种访问方式 两种类型复制 函数参数的传递 两种变量类型检测 回到目录 数据类型 1. ECMAScript变量包含两种不同类型的值:基本类型值.引用类型值: 2. 基 ...
- 高手用的SourceInsight配置文件——仿Sublime风格【转】
本文转载自:https://blog.csdn.net/weixin_38233274/article/details/80209100 配置文件下载地址:https://download.csdn. ...
- openwrt生成的镜像放在哪里
答:1.打包好之后是放在build_dir/target-$(cross-compile-toolchan-name)/linux-$(chip-series-name)_$(chip-arch)/t ...
- win10家庭版的defender注册表关闭和开启
关闭方法: 打开“命令提示符(管理员)”,然后输入: reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defe ...
- LeetCode——Longest Word in Dictionary through Deleting
1. Question Given a string and a string dictionary, find the longest string in the dictionary that c ...
- [小问题笔记(一)] js关闭页面window.close() 无效,不弹提示直接关闭
window.close(); 单独写这句,各个浏览器效果都不一样.比如IE(不是所有版本)会弹提示: 点确定之后才会关闭.而有些浏览器压根就没反应. 需要让它不弹提示,直接关闭,在window.cl ...
- C#代码实现 Excel表格与Object互相转换,Excel表格导入数据库(.NET2.0 .NET4.0)
前些天在工作上遇到这个需求,在GitHub找到一个开源代码可以用,Fork了一个版本,整理一下发出来. ①.Net项目中使用Nuget安装一个 NPOI 包 https://github.com ...