【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 ...
随机推荐
- hdu 5211 Mutiple 数学
Mutiple Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5211 ...
- 【转】OpenCV Mat数据结构
转载自xiahouzuoxin原文 OpenCV基础篇之Mat数据结构 程序及分析 /* * FileName : MatObj.cpp * Author : xiahouzuoxin @163.co ...
- nodejs 服务器 崩溃 2种解决办法
用node启动server后,发现服务器不稳定,经常crash.我是用ssh远程登录的,ssh远程通道中断,或者Ctrl+C,都会使nodejs server崩溃掉. 一,node server 崩溃 ...
- BigDecimal 执行精确小数计算
来考虑这样一种情况,先来看代码: public class Test { public static void main(String[] args) { System.out.println(0.4 ...
- 完整的ASP.NET存储过程分页,排序,鼠标移至变色
首先建立一个存储过程如下(MySQL数据库):CREATE DEFINER=`root`@`localhost` PROCEDURE `pagination`( in tbName var ...
- 关于OGRE与OSG的简单比较【转】
关于OGRE与OSG的简单比较 林乃养 lnychina{at}gmail.com 浙江大学CAD&CG实验室 2010年3月27日 1 前言 我曾经细致阅读过OGRE和OSG官方提供的文档, ...
- IOS 预处理语句
程序中的源代码计算机是无法识别的,需要将写好的代码转成0.1二进制代码,计算机才能识别.将源代码转成二进制代码的需要经过两步,编译和链接.编译是通过编译器将每个文件的代码都转为二进制代码,在这个过程中 ...
- 使用 Python 创建你自己的 Shell (上)
我很想知道一个 shell (像 bash,csh 等)内部是如何工作的.于是为了满足自己的好奇心,我使用 Python 实现了一个名为yosh(Your Own Shell)的 Shell.本文章所 ...
- Linux的防火墙–Iptables
导读 Iptable已经集成在Linux 2.4及以上版本的内核中,同Windows下的众多“傻瓜”防火墙不同的是,Iptables需要用户自己定制相关规则.下面我就给大家简单介绍一下关于防火墙的基本 ...
- Windows 批处理 ping 某个网段
原文: https://blog.csdn.net/leuxcn/article/details/51288248 ------------------------------------------ ...