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. wait, WIFEXITED, WEXITSTATUS

    wait, WIFEXITED, WEXITSTATUS     偶尔翻到了这几个关键字,找到个文章复习了下:“点我”.记录下: wait的函数原型是: #include <sys/types. ...

  2. CentOS下KVM克隆完成后修改MAC地址/VMware复制虚拟机修改MAC地址

    克隆完成之后可能mac地址会有冲突,进入KVM删除/etc/udev/rules.d/70-persistent-net.rules中的eth0的配置,接着把eth1改成eth0,并且修改/etc/s ...

  3. object-c的http post请求之 ASIFormDataRequest使用

    ASIHTTPRequest类库中的ASIFormDataRequest是实现HTTP协议中的处理POST表单的很好的类库.使用起来非常简单. 在说明之前先需要了解HTTP请求的Get和Post方法. ...

  4. Can a windows dll retrieve its own filename?

    http://stackoverflow.com/questions/2043/can-a-windows-dll-retrieve-its-own-filename A windows exe fi ...

  5. 探究rh6上mysql5.6的主从、半同步、GTID多线程、SSL认证主从复制

    http://407711169.blog.51cto.com/6616996/1203973/

  6. std::string compare

    #include <iostream> #define NULL 0 #define MAX_LIMIT 5 //#define MAX_LENGTH 2 bool ComparePC2S ...

  7. jQuery向父辈遍历的方法

      通过DOM树可以可容易的访问到html文档中的所有元素 例如向上访问父辈的元素有以下方法 1.parent()方法可以得到所定元素的直接父元素 $("span").parent ...

  8. linux查看端口被哪个服务占用的命令

    netstat -tunpl | grep 6379

  9. RxJava 和 RxAndroid (生命周期控制和内存优化)

    RxJava使我们很方便的使用链式编程,代码看起来既简洁又优雅.但是RxJava使用起来也是有副作用的,使用越来越多的订阅,内存开销也会变得很大,稍不留神就会出现内存溢出的情况,这篇文章就是介绍Rxj ...

  10. jquery点击回到页面顶部方法

    1.代码实例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...