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.

 
 
使用dpHeight[]数组来记录到第i行为止,第j个位置垂直连续包含多少个1(包括matrxi[i][j])。如:

1 0 1 1 0

1 1 0 1 0

0 1 1 1 1

有如下结果:

第1行: dpHeight[] = {1, 0, 1, 1, 0}

第2行: dpHeight[] = {2, 1, 0, 2, 0}

第3行: dpHeight[] = {0, 2, 1, 3, 0}

 
对每个dpHeight求最大矩形面积,利用Largest Rectangle in Histogram里面的求解方法即可
 
  

 class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) { int m=matrix.size();
if(m==) return ;
int n=matrix[].size(); vector<int> dpHeight(n,); int result=;
for(int i=;i<m;i++)
{
for(int j=;j<n;j++)
{
if(matrix[i][j]=='') dpHeight[j]++;
else dpHeight[j]=;
} int tmp=lineMaximalRectangle(dpHeight);
if(tmp>result) result=tmp;
} return result;
} struct Node
{
int index;
int height;
Node(int i,int h):index(i),height(h){};
}; int lineMaximalRectangle(vector<int> &dpHeight)
{ int len=dpHeight.size();
if(len==) return ; stack<Node> stk;
Node n(,dpHeight[]);
stk.push(n); int result=;
for(int i=;i<len;i++)
{
int height=dpHeight[i]; if(height>=stk.top().height)
{
stk.push(Node(i,height));
}
else
{
int nodeIndex=i;
while(!stk.empty()&&height<stk.top().height)
{
int tmp=(i-stk.top().index)*stk.top().height;
if(tmp>result) result=tmp;
nodeIndex=stk.top().index;
stk.pop();
}
stk.push(Node(nodeIndex,height));
}
}
while(!stk.empty())
{
int index=stk.top().index;
int height=stk.top().height;
int tmp=(len-index)*height;
if(tmp>result) result=tmp;
stk.pop();
} return result; }
};
 

【leetcode】Maximal Rectangle的更多相关文章

  1. 【leetcode】Maximal Rectangle (hard)★

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  2. 【LeetCode】223. Rectangle Area 解题报告(Python)

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

  3. 【LeetCode】836. Rectangle Overlap 解题报告(Python)

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

  4. 【LeetCode】223 - Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  5. 【Leetcode】Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

  7. 【LeetCode】栈 stack(共40题)

    [20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...

  8. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  9. 【leetcode】1019. Next Greater Node In Linked List

    题目如下: We are given a linked list with head as the first node.  Let's number the nodes in the list: n ...

随机推荐

  1. 一些总结application和事务

    如果需要添加一个全局变量的话  放到application中,但是这个变量中的值要修改的话,必须重新设置application里的值,要不就得重启服务器. String SiteName = (Str ...

  2. JS从剪切板里粘贴图片

    功能需求:在网页中,Ctrl+V,把系统剪切板的图片(比如QQ截图)进行粘贴.显示.上传...,提高用户体验. 参考链接:https://ruby-china.org/topics/17266 git ...

  3. jQuery回调、递延对象总结(下篇) —— 解密jQuery.when方法

    前言: 前一篇文章中重点总结了一下then方法,它主要用来处理多个异步任务按顺序执行,即前一个任务处理完了,再继续下一个,以此类推: 而这一章节jQuery.when方法也是处理多个异步任务,它把多个 ...

  4. jquery选择器(二)-层次选择器

    1. 后代选择器 2. 子元素选择器 3. 相邻兄弟元素选择器 4. 一般兄弟元素选择器(同辈元素选择器) 5. 补充(除自身以外的前后同辈选择器)

  5. ubuntu安装php常见错误集锦

    一.configure 报错 1.错误类型: Configure: error: Please reinstall the libcurl distribution-easy.h should be ...

  6. Android工程文件下assets文件夹与res文件夹的区别

    1.assets:不会在R.java文件下生成相应的标记,assets文件夹可以自己创建文件夹,必须使用AssetsManager类进行访问,存放到这里的资源在运行打包的时候都会打入程序安装包中, 2 ...

  7. BZOJ2049——[Sdoi2008]Cave 洞穴勘测

    1.题目大意:就是一个动态维护森林联通性的题 2.分析:lct模板题 #include <stack> #include <cstdio> #include <cstdl ...

  8. h5上传图片

    1.如何在H5上传图片 使用FileReader 2.FileReader接口 传图片我们只用到readAsDataURL 3.FileReader接口事件 传图片我们只用到onload 4.如何使用 ...

  9. [转载]Web 研发模式演变

    原文链接:https://github.com/lifesinger/blog/issues/184 前不久徐飞写了一篇很好的文章:Web 应用的组件化开发.本文尝试从历史发展角度,说说各种研发模式的 ...

  10. Sqli-LABS通关笔录-4

    这一关卡让我学习到了 1.管他如何,想方设法先让sql报错再说.从报错中构造sql注入语句. 2.单引号不行就来双引号.括号等等的. 这次单引号没反应了.以后我们先来黑盒测试,实在没辙再看代码. -1 ...