【LeetCode】85. Maximal Rectangle
Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
如果用DP来做,判断(begin1,end1)~(begin2,end2)范围是否全1,会超时。
对于矩阵matrix,逐行构建height数组,调用Largest Rectangle in Histogram即可。
对matrix第i行构建height数组方法:对于height[j],即从matrix[i][j]开始,最多到达matrix[0][j]的连续1个数。
class Solution {
public: int maximalRectangle(vector<vector<char> > &matrix) {
int ret = ;;
if(matrix.empty() || matrix[].empty())
return ret;
int m = matrix.size();
int n = matrix[].size();
for(int i = ; i < matrix.size(); i ++)
{
vector<int> height(n, );
for(int j = ; j < n; j ++)
{
int r = i;
while(r >= && matrix[r][j] == '')
{
height[j] ++;
r --;
}
}
ret = max(ret, largestRectangleArea(height));
}
return ret;
} int largestRectangleArea(vector<int> &height) {
if(height.empty())
return ; int result = ;
stack<int> s; //elements in stack s are kept in ascending order
int ind = ;
while(ind < height.size())
{
if(s.empty() || height[ind]>=s.top())
{
s.push(height[ind]);
}
else
{
int count = ; //pop count
while(!s.empty() && height[ind]<s.top())
{
int top = s.top();
s.pop();
count ++;
result = max(result, count*top);
}
for(int i = ; i <= count; i ++)
s.push(height[ind]); //push count+1 times
}
ind ++;
}
//all elements are in stack s, and in ascending order
int count = ;
while(!s.empty())
{
count ++;
int top = s.top();
s.pop();
result = max(result, count*top);
} return result;
}
};
【LeetCode】85. Maximal Rectangle的更多相关文章
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
- 【一天一道LeetCode】#85. Maximal Rectangle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】085. Maximal Rectangle
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's ...
- LeetCode OJ 85. Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle
问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
- 【LeetCode】84. Largest Rectangle in Histogram
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【leetcode】Contains Duplicate & Rectangle Area(easy)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
随机推荐
- 关于收到谷歌邮件 Googlebot can't access your site 的解决方法
最近一段时间一直都收到谷歌的邮件,而且“新锐工作室”的关键字在谷歌收录及排名都没有了.下面图为谷歌蜘蛛无法抓取网站的截图,如果你在谷歌网管工具里收到类似消息,说明也中招了.[Webmaster Too ...
- Supported_Hardware#4G_LTE_cards_and_modems
https://wiki.mikrotik.com/wiki/Supported_Hardware#4G_LTE_cards_and_modems
- linux下的系统调用函数到内核函数的追踪
http://blog.csdn.net/maochengtao/article/details/23598433
- JS返回数组种类和个数(面试常问)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Ext树形结构
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Enable Notepad++ 666 support both SCLEX_FORTRAN and SCLEX_F77
Enable Notepad++ 666 support both SCLEX_FORTRAN and SCLEX_F77 http://blog.csdn.net/gocad/article/det ...
- mock的概念
1 现有的单元测试框架单元测试是保证程序正确性的一种有效的测试手段,对于不同的开发语言,通常都能找到相应的单元框架. 借助于这些单测框架的帮助,能够使得我们编写单元测试用例的过程变得便捷而优雅.框架帮 ...
- slf4j log4j logback log4j2关系详解和相关用法
来源:slf4j log4j logback关系详解和相关用法https://www.cnblogs.com/Sinte-Beuve/p/5758971.html The Simple Logging ...
- Delphi第三方组件安装DCU.PAS.DPK.BPL.ActiveX控件
不是由BORLAND提供的组件叫第三方组件:安装方法:南山古桃(nsgtao)首先提醒一下:最好把要安装的文件先复制到Delphi安装目录的Lib目录下再执行下面的操作!就目前常见的各种形式的组件的安 ...
- powerVR tbdr 硬件架构理解
有两点 TSP里的iteration 和那个vertex data 到 tsp的 *2 itedration是为了把 ps里面uv的动态改变提前算出来给 texture fetch用 这个的通用方法是 ...