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 ...
随机推荐
- Spring DI - 依赖注入
1.IOC(DI) - 控制反转(依赖注入) 所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管理过程交由Spring框架来处理,从此在开发过程中不再需要关注对象的创建和生 ...
- R语言学习——欧拉计划(11)Largest product in a grid
Problem 11 In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 0 ...
- [转]NSIS:判断D盘存在与否确定安装路径
转载自:http://www.flighty.cn/html/bushu/20140704_239.html 现在我们想实现这样的功能: 如果目标机器存在D盘,那么就安装程序到D盘,否则安装在系统 ...
- Js/jquery获取当前日期时间及其它操作
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...
- Linux 期中架构 MySQL
MySQL基础部分 mysql安装脚本 安装前请将相关安装包copy放到/server/tools目录下 -rw-r--r-- 1 root root 314149697 Mar 23 2017 ...
- linux shell 命令常用快捷键
下面是一些shell的常用快捷键,快捷键玩熟悉了在一定程度上是可以提高工作效率滴… Ctrl + a 切换到命令行开始 Ctrl + e 切换到命令行末尾 Ctrl + l 清除屏幕内容 Ctrl + ...
- 初步认识AutoMapper
AutoMapper 初步认识AutoMapper 前言 手动映射 使用AutoMapper 创建映射 Conventions 映射到一个已存在的实例对象 前言 通常在一个应用程序中,我们开发 ...
- python的XML处理模块ElementTree
ElementTree是python的XML处理模块,它提供了一个轻量级的对象模型.它在Python2.5以后成为Python标准库的一部分,但是Python2.4之前需要单独安装.在使用Elemen ...
- ORM创建 脚本运行
- HTML5 学习之地理定位
html5 获取坐标: <!DOCTYPE HTML> <html> <head> <title>test1.html</title> &l ...