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. 金融即服务(FaaS),将开启场景化金融新格局

    转自: https://www.iyiou.com/p/28494/fs/1 [ 亿欧导读 ] 金融即服务揭示了场景金融的实现路径,通过双向连接做一个开放的系统,按需给客户提供金融服务. 本文系作者在 ...

  2. spring cloud 转

    http://blog.csdn.net/forezp/article/details/70148833 服务的注册与发现(Eureka) 服务注册(consul) 服务消费者(rest+ribbon ...

  3. oracle同一个库上面,不同用户相互赋予权限

    用法: 有两个用户:user1和user2,都是在库TEST上,分别有表user1.table1,user2.table2 但是用user1登录的时候,user2上表就不能用,此时就可以使用grant ...

  4. 《阿里巴巴Java开发规约》插件使用

    通过Jetbrains官方仓库安装 1. 打开 Settings >> Plugins >> Browse repositories... 2. 在搜索框输入alibaba即可 ...

  5. jquery map方法

    jQuery.map( array, callback(elementOfArray, indexInArray) )Returns: Array 感觉jquery的map方法非常好用,特向大家分享下 ...

  6. rhel7配置链路聚合(双网卡热备)

    team方法 1). nmcli connection add type team con-name team0 ifname team0 config '{"runer":{&q ...

  7. 爬虫框架Scrapy之案例二

    新浪网分类资讯爬虫 爬取新浪网导航页所有下所有大类.小类.小类里的子链接,以及子链接页面的新闻内容. 效果演示图: items.py import scrapy import sys reload(s ...

  8. TIME_WAIT和CLOSE_WAIT

    先看下三次握手四次挥手的状态变化: 通常会遇到下面两种情况: 服务器保持了大量TIME_WAIT状态 服务器保持了大量CLOSE_WAIT状态 因为linux分配给一个用户的文件句柄是有限的,而TIM ...

  9. JS中函数定义和函数表达式的区别

    摘要: (function() {})();和(function(){}());的区别 Javascript中有2个语法都与function关键字有关,分别是: 函数定义:function Funct ...

  10. 开发H5游戏引擎的选择:Egret或Laya?

    开发H5游戏引擎的选择:Egret或Laya? 一.总结 一句话总结:选laya吧 二.开发H5游戏引擎的选择:Egret或Laya? 一.H5游戏开发的引擎介绍 开发H5游戏的引擎有很多,比如egr ...