85. Maximal Rectangle (Graph; Stack, DP)
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)的更多相关文章
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 【LeetCode】85. Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- 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 ...
随机推荐
- 去除adb传输中的^M
学习sed过程中,在文本中每行追加内容,发现使用adb会在行末追加一个看不到^M. 场景一:adb保存到文件 adb shell ps|head -n 10 > text.txt,使用sed进行 ...
- 大家都对vertical-align的各说各话
原文地址:http://www.blueidea.com/tech/web/2008/5892.asp 最近几天仔细研究了一下vertical-align这个属性,结果让我大吃一惊,这个很“资深”的C ...
- 1053 Path of Equal Weight (30 分)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- 关于json_encode()的使用注意
json_encode($json_str,true)在一般情况下可以返回一个数组,但当$json_str的字符编码是GBK或其它时,返回的是一个 空数组,必须用iconv(‘gbk’,‘ut8//I ...
- 用cmd导入oracle的.dmp文件和修改oracle管理员密码
1,首先创建用户 语法[创建用户]: create user 用户名 identified by 口令[即密码]: 例子:create user zhengxin identified by zhen ...
- keras中调用tensorboard:from keras.callbacks import TensorBoard
from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn ...
- javascript节点操作replaceChild()
replaceChild(a,b)是用来替换文档中的已有元素的 参数a:要插入的节点, 参数b:要替换的节点 var oDiv = document.getElementById("guoD ...
- 初学 python 之 用户登录实现过程
要求编写登录接口 : 1. 输入用户名和密码 2.认证成功后显示欢迎信息 3.用户名输错,提示用户不存在,重新输入(5次错误,提示尝试次数过多,退出程序) 4.用户名正确,密码错误,提示密码错误,重新 ...
- mysql sleep连接过多解决办法
睡眠连接过多,会对mysql服务器造成什么影响? 严重消耗mysql服务器资源(主要是cpu, 内存),并可能导致mysql崩溃. 造成睡眠连接过多的原因? 1. 使用了太多持久连接(个人觉得,在高并 ...
- PHP提取多维数组指定一列的方法大全
目录 1 array_column函数法 2 array_walk函数法 3 array_map函数法 4 foreach循环法 5 array_map变种 PHP中对多维数组特定列的提取,是个很常用 ...