[LintCode] Maximal Rectangle 最大矩形
Given a 2D boolean matrix filled with False and True, find the largest rectangle containing all True and return its area.
Example
Given a matrix:
[
[1, 1, 0, 0, 1],
[0, 1, 0, 0, 1],
[0, 0, 1, 1, 1],
[0, 0, 1, 1, 1],
[0, 0, 0, 0, 1]
]
return 6.
LeetCode上的原题,请参见我之前的博客Maximal Rectangle。
解法一:
class Solution {
public:
/**
* @param matrix a boolean 2D matrix
* @return an integer
*/
int maximalRectangle(vector<vector<bool> > &matrix) {
if (matrix.empty() || matrix[].empty()) return ;
int res = , m = matrix.size(), n = matrix[].size();
vector<int> h(n + , );
for (int i = ; i < m; ++i) {
stack<int> s;
for (int j = ; j < n + ; ++j) {
if (j < n) {
if (matrix[i][j]) ++h[j];
else h[j] = ;
}
while (!s.empty() && h[s.top()] >= h[j]) {
int cur = s.top(); s.pop();
res = max(res, h[cur] * (s.empty() ? j : (j - s.top() - )));
}
s.push(j);
}
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param matrix a boolean 2D matrix
* @return an integer
*/
int maximalRectangle(vector<vector<bool> > &matrix) {
if (matrix.empty() || matrix[].empty()) return ;
int res = , m = matrix.size(), n = matrix[].size();
vector<int> h(n, ), left(n, ), right(n, n);
for (int i = ; i < m; ++i) {
int cur_left = , cur_right = n;
for (int j = ; j < n; ++j) {
h[j] = matrix[i][j] ? h[j] + : ;
}
for (int j = ; j < n; ++j) {
if (matrix[i][j]) left[j] = max(left[j], cur_left);
else {left[j] = ; cur_left = j + ;}
}
for (int j = n - ; j >= ; --j) {
if (matrix[i][j]) right[j] = min(right[j], cur_right);
else {right[j] = n; cur_right = j;}
}
for (int j = ; j < n; ++j) {
res = max(res, (right[j] - left[j]) * h[j]);
}
}
return res;
}
};
[LintCode] Maximal Rectangle 最大矩形的更多相关文章
- [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 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 085 Maximal Rectangle 最大矩形
给定一个填充了 0 和 1 的二进制矩阵,找到最大的只包含 1 的矩形并返回其面积.例如,给出以下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 6 详见:http ...
- LintCode 510: Maximal Rectangle
LintCode 510: Maximal Rectangle 题目描述 给你一个二维矩阵,权值为False和True,找到一个最大的矩形,使得里面的值全部为True,输出它的面积 Wed Nov 2 ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 最大的矩形面积 Maximal Rectangle
2018-09-15 10:23:44 一.Largest Rectangle in Histogram 在求解最大的矩形面积之前,我们先讨论一条最大直方图面积的问题. 问题描述: 问题求解: 解法一 ...
- LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)
84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 47. Largest Rectangle in Histogram && Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
随机推荐
- 11g新特性-如何禁用自动统计信息收集作业
一.11g中auto stats gather job被集成到了auto task中. SQL> select client_name,status from DBA_AUTOTASK_CLIE ...
- Android学习网站
1 <老罗Android应用开发视频教程> http://www.mobiletrain.org/about/news/android_video2.html
- hdu 5306 优先队列
用到优先队列 #include<iostream> #include<string> #include<algorithm> #include<cstdio& ...
- java 汽车销售收入系统
>>>>>>>>>>>>>>>>>>>> 语言:java 工具:eclipse ...
- css整理-01选择器和继承
元素 元素形式: 替换,非替换 元素类型: 块级,行内 列表是特殊的块级元素,它会生成一个标记符 样式表 候选样式表: rel='alternative' @import导入样式表,必须在style的 ...
- js-其他
- delphi 对Tmemo指定的行写入
mmoMonitor:Tmemo; mmoMonitor.Lines.ValueFromIndex[0]:=aInfo ; procedure TMainForm.LogInfo(aInfo: str ...
- JS(ajax笔记)
简介:AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法,是与服务器交 ...
- 遍历table指定name的td
$("td[name='rates']").each(function () { var temp = $(this).text().substr(0,$(this).text() ...
- http://zhidao.baidu.com/link?url=X7IUn1KtjVb0889-lR1OlNOl5xJaA49LEqPHvjTvfKJt5uXPsyi-sn-Xc-yw6-fbaIBvuF0MiTVZGpZGeoW_HLphIR5WmiMVDMoNBFAOINa
http://zhidao.baidu.com/link?url=X7IUn1KtjVb0889-lR1OlNOl5xJaA49LEqPHvjTvfKJt5uXPsyi-sn-Xc-yw6-fbaIB ...