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. 论文笔记:蒸馏网络(Distilling the Knowledge in Neural Network)

    Distilling the Knowledge in Neural Network Geoffrey Hinton, Oriol Vinyals, Jeff Dean preprint arXiv: ...

  2. 基于session和token的身份认证方案

    一.基于session的身份认证方案 1.方案图示 2.比较通用的鉴权流程实现如下: 在整个流程中有两个拦截器. 第一个拦截器AuthInteceptor是为了每一次的请求的时候都先去session中 ...

  3. windows使用IPC和文件共享

    远程访问windows资源有很多方式,如果给自己用可以使用ipc或开启共享设置只共享给特定用户.如果给所有人用,可以开启everyone共享和guest账户 { "远程获取Windows资源 ...

  4. [.net基础]访问修饰符

    标题:[.net基础]访问修饰符 一.前言 基础掌握不牢固啊,所以记录下来. 二.方法访问修饰符Internal (1).创建工程ParentAndSon (2).添加类ModelA namespac ...

  5. 20145310 《Java程序设计》第4周学习总结

    20145310 <Java程序设计>第4周学习总结 教材学习内容总结 本周主要进行第五章和第六章的学习. 第六章 继承与多态 子类(Inherit)继承父类,避免重复的行为定义,不过并非 ...

  6. Pomelo热更新刷新handler和remote 以及 pomelo使用bearcat进行热更新

    一. 开启 原生 pomelo 的hotreload支持 pomelo版本: 2.2.5 , 编辑脚本 app.js 加入如下代码 //全局配置 app.configure('production|d ...

  7. backstopJS 参数详解 CN

    最近需要进行前端UI验证,详细研究了下backstopJS.官方文档为En,手动翻译了下.相关参数信息如下: 如需转载引用,请保留原文出处,支持原创,感谢.

  8. Package Manager Console的使用

    Find-Package PM> Find-Package autofac https://docs.microsoft.com/en-us/nuget/tools/ps-ref-find-pa ...

  9. UVA 12657 Boxes in a Line(双向链表+小技巧)

    题意:对于一行按照顺序排列盒子数字与位置都为 1,2,3,4....n 执行四种操作 c = 1    x 放到 y 的左边 c =2     x 放到 y 的右边 c =3 交换 x, y c =4 ...

  10. Pandas 高级应用 数据分析

    深入pandas 数据处理 三个阶段 数据准备 数据转化 数据聚合 数据准备 加载 组装 合并 - pandas.merge() 拼接 - pandas.concat() 组合 - pandas.Da ...