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

思路:

例如

01101

11010

01110

11110

11111

00000

按列从上到下计算maximal rectangle:

01101

12010

03110

14210

25321

00000

然后对每一行求直方图的最大面积,于是这个问题可以转化为Largest Rectangle in Histogram。

class Solution {
public:
int largestRectangleArea(vector<int> &height) {
if(height.size() == ) return ; int res = ;
stack<int> idxStack;
height.push_back(); //为了如果在最后height为最高的情况,能够再进一次else,把stack中的元素pop出来计算 for(int i = ; i < height.size(); i++)
{
if(idxStack.empty() || (!idxStack.empty() && height[i] >= height[idxStack.top()])) //当前高度>=栈顶高度
idxStack.push(i); //入栈
else{ //高度降低了,那么再之后也就不可能超过height[idx],所以看之前的高度*宽度能够达到怎样的值
while(!idxStack.empty() && height[idxStack.top()] > height[i]) //只要当前高度<栈顶高度
{
int idx = idxStack.top();
idxStack.pop();
int width = idxStack.empty() ? i : (i-idxStack.top()-); //当前index-1的位置(目前为止最高高度的位置)到当前栈顶元素的位置的宽度
res = max(res, height[idx] * width);
}
idxStack.push(i);
}
}
height.pop_back();
return res;
} int maximalRectangle(vector<vector<char> > &matrix) {
if (matrix.size() < ) return ;
int n = matrix.size();
if (n == ) return ;
int m = matrix[].size();
if (m == ) return ;
vector<vector<int> > lines(n, vector<int>(m, ));
for (int i = ; i < n; ++i) {
for (int j = ; j < m; ++j) {
if (i == ) {
lines[i][j] = ((matrix[i][j] == '') ? : );
} else {
lines[i][j] += ((matrix[i][j] == '') ? lines[i-][j] + : );
}
}
}
int maxRec = , tmpRec;
for (int i = ; i < n; ++i) {
tmpRec = largestRectangleArea(lines[i]);
maxRec = (maxRec > tmpRec) ? maxRec : tmpRec;
}
return maxRec;
}
};

85. Maximal Rectangle (Graph; Stack, DP)的更多相关文章

  1. 刷题85. Maximal Rectangle

    一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...

  2. 85. Maximal Rectangle

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

  3. 【LeetCode】85. Maximal Rectangle

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

  4. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

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

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

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

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

  7. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

  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. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

    1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...

随机推荐

  1. shell 8printf

    printf printf使用引用文本或空格分隔的参数,外面可以在printf中使用格式化字符串,还可以制定字符串的宽度.左右对其方式等.printf不会像echo自动添加换行符,因此需要手动添加\n ...

  2. js中级总结

    this问题: this是JavaScript的关键字      用途:指向某一个对象 如何判断this的指向 函数内:两种情况:1.以函数形式调用(不带 . 指向window ) 2.以方法形式调用 ...

  3. MySQL concat用法举例

    concat配合information_schema的应用 1    concat的一般用法主要是用于拼接 示例: 执行语句 SELECT CONCAT('M','y','S','Q','L') 可以 ...

  4. [UE4]C++取得蓝图控件实例

    .h /*确认密码输入框*/ UPROPERTY() UEditableTextBox* EditableTextBoxRePassword; .cpp EditableTextBoxPassword ...

  5. ThinkPHP 5使用 Composer 组件名称可以从https://packagist.org/ 搜索到

    http://www.phpcomposer.com/ 1 这个是国内的composer网站 thinkphp5自带了composer.phar组件,如果没有安装,则需要进行安装 以下命令全部在项目目 ...

  6. 经典收藏链接(C#总结)

    去年底转到Java,在此总结一下.很多不错的C#博客在此收藏标记\(^o^)/~ 1.基础知识 Linq专题:http://www.cnblogs.com/RuiLei/archive/2009/09 ...

  7. Linux下apache2及模块mod_deflate等安装和配置

    安装apache 1.wget http://archive.apache.org/dist/httpd/httpd-2.2.13.tar.gz 2.在安装目录 先让大家看看实际效果,请看下图10点中 ...

  8. OTL技术应用

    什么是OTL:OTL 是 Oracle, Odbc and DB2-CLI TemplateLibrary 的缩写,是一个操控关系数据库的C++模板库,它目前几乎支持所有的当前各种主流数据库,如下表所 ...

  9. OpenACC 书上的范例代码(Jacobi 迭代),part 2

    ▶ 使用Jacobi 迭代求泊松方程的数值解 ● 首次使用 OpenACC 进行加速,使用动态数组,去掉了误差控制 #include <stdio.h> #include <stdl ...

  10. 基于二进制RPC协议法的轻量级远程调用框架 ---- Hessian

    使用Java创建Hessian服务有四个步骤: 1.创建Java接口作为公共API                             (client和server端 创建一个相同的借口) 2.使 ...