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的更多相关文章

  1. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

  2. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

  3. 【一天一道LeetCode】#85. Maximal Rectangle

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】085. Maximal Rectangle

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's ...

  5. 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 ...

  6. 【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle

    问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...

  7. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  8. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

  9. 【leetcode】Contains Duplicate & Rectangle Area(easy)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

随机推荐

  1. Lnmmp

    简介 LNMMP=Linux+Nginx+MySQL+Memcache+PHP: 利用Nginx的高性能特点做前端反向代理服务器,分发用户请求,并在能够利用缓存的地方使用Memcache缓存服务,以加 ...

  2. 面试&笔试---c语言之字符串处理

    1.字串拷贝库函数strcpy 函数介绍: 原型声明:extern char *strcpy(char *dest,const char *src); 头文件:string.h 功能:把从src地址开 ...

  3. undefined null 各种值比较(面试题)

    undefined和null与任何有意义的值比较返回的都是false,但是null与undefined之间互相比较返回的是true. console.log(null == false); //fal ...

  4. Windows UWP开发系列 – MessageDialog 和 ContentDialog

    MessageDialog 在Metro程序中,没有传统的窗口,当我们要用需要交互的消息提示时,在Win8时代,引入了一个MessageDialog来取代常用的MessageBox.使用方法如下: p ...

  5. How to create functions that can accept variable number of parameters such as Format

    http://www.chami.com/tips/delphi/112696D.html Sometimes it's necessary to pass undefined number of [ ...

  6. wpf 分别用 xaml 和后台代码实现 色彩渐变

    xaml 方法: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.micros ...

  7. directio mysql 编绎选项

    http://www.myexception.cn/linux-unix/495407.html http://www.iyunv.com/thread-25950-1-1.html

  8. Eclipse开发Java程序入门,HelloWord

    今天看到一个专业的学术程序是Java写的,而我经常用的是Matalb,所以感觉十分不舒服.之前学的Java开发感觉也忘光了,所以感觉,知识必须要总结好,不然容易忘记.这也就是我写这篇文章的原因.希望能 ...

  9. Spark1.0.0 应用程序部署工具spark-submit

    原文链接:http://blog.csdn.net/book_mmicky/article/details/25714545 随着Spark的应用越来越广泛,对支持多资源管理器应用程序部署工具的需求也 ...

  10. iOS: 沙盒的详解和目录的获取

    沙盒的详解: •iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被称为沙盒 •iOS常用目录: –Bundle       //该目录下的文件是用来存储应用程序包的 ...