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(最大矩形)的更多相关文章

  1. [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 ...

  2. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  3. 【leetcode】Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  4. [LintCode] Maximal Rectangle 最大矩形

    Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True ...

  5. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  6. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

  7. 【leetcode】Maximal Rectangle (hard)★

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  8. leetcode[85] Maximal Rectangle

    给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...

  9. LeetCode OJ 223.Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  10. [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 ...

随机推荐

  1. java.lang.ClassFormatError: Trjava.lang.ClassFormatError: Truncated class fileuncated class file

    周末过来加班,上传编译好的文件后,部署到服务器没事.但是服务器日志满了,把日志清除后,把服务启动,发现报这个错误,大致网上看了一下,这个错误是编译的文件损坏了.然后大致看了一下文件,还真是.由于日志满 ...

  2. Nginx配置性能优化(转)

    原文地址:http://blog.csdn.net/xifeijian/article/details/20956605 高层的配置 nginx.conf文件中,Nginx中有少数的几个高级配置在模块 ...

  3. Atom中设置你的Snippet,atom技巧(二)

    我们经常需要对二维数组进行迭代,比如这样: for(int x = 0; x < width; x++){ for(int y = 0; y < height; y++){ screen[ ...

  4. Linux学习笔记之Xshell配色方案定制

    点击 Xshell 面板顶部的如下按钮. 点击 Browse 按钮,弹出如下面板,选择 ANSI Colors on Black,然后点击右侧save as 按钮,命名为 zkl.   这里其实就是复 ...

  5. Clonal hematopoiesis of indeterminate potential(意义不明的克隆性造血)-CHIP

    意义不明的克隆性造血是指由一个造血干细胞或者其他早期的起始血细胞为了更好的适应环境而发展成一个带有一些基因变异的亚型. 这个亚型带有基因变异一般是非驱动性的,而且该亚型占血细胞的比率跟年龄有很大的相关 ...

  6. 照着官网来安装openstack pike之安装dashboard

    上文提到了利用命令行下使用openstack的命令来创建虚拟机,这里选择安装dashboard来安装基于web界面的openstack平台 利用dashboard界面来创建虚拟机 dashboard这 ...

  7. rocketMQ基本理解

    消息中间件需要解决哪些问题? Publish/Subscribe 发布订阅是消息中间件的最基本功能,也是相对于传统RPC通信而言. Message Priority 规范中描述的优先级是指在一个消息队 ...

  8. Hive-0.13安装

    Hive只需在使用节点安装即可. 1.上传tar包.解压   tar -zxvf apache-hive-0.13.0-bin.tar.gz -C /hadoop/  配置HIVE_HOME环境变量  ...

  9. openwrt中的append-ubi定义在哪里

    include/image-commands.mk 定义如下: define Build/append-ubi sh $(TOPDIR)/scripts/ubinize-image.sh \ $(if ...

  10. SpringMVC基于注解方式的quartz

    项目框架: SpringMVC.MyBatis.JSP 1. 首先配置spring.xml文件 <?xml version="1.0" encoding="UTF- ...