【leetcode】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.
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}
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的更多相关文章
- 【leetcode】Maximal Rectangle (hard)★
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 【LeetCode】223. Rectangle Area 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rectangl ...
- 【LeetCode】836. Rectangle Overlap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rectangle ...
- 【LeetCode】223 - Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- 【Leetcode】Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- 【LeetCode】哈希表 hash_table(共88题)
[1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【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 ...
随机推荐
- 小技能——markdown
如果常常要在电脑上写点东西,比如写笔记.做总结.写博客之类的,花一两个小时学会markdown还是很值的. markdown简介 markdown不是某个软件,而是一种标记语言,标记普通文本的格式,以 ...
- gradle init.gradle的文件配置 使用
init.gradle文件在build开始之前执行,所以你可以在这个文件配置一些你想预先加载的操作例如配置build日志输出.配置你的机器信息,比如jdk安装目录,配置在build时必须个人信息,比如 ...
- 本地自定义了404 和500 错误处理 部署到IIS上显示 服务器内部错误
问题如图 解决办法如下,在IIS上设置一下即可
- SQL笔记 - 解决CTE定位点类型和递归部分的类型不匹配
在CTE递归测试,也就是部门名称拼接的时候,遇到了小问题: 登时就迷糊了:不都是取的是Unit表中的同一个列,相加之后类型就变了么? 难道是因为,系统知道这是在进行递归运算,但又不确定递归的层次,以及 ...
- 关于JS的几点TIPS
作为前端基本工作每天都会用到JS...但是我们对JS真的都了解吗,或者说有什么tips是我们不知道的呢.. So..此文关于JS的几点tips..... 一:定时器(可传多个参数) 首先是一个一般的定 ...
- 图解Tomcat类加载机制
说到本篇的tomcat类加载机制,不得不说翻译学习tomcat的初衷. 之前实习的时候学习javaMelody的源码,但是它是一个Maven的项目,与我们自己的web项目整合后无法直接断点调试.后来同 ...
- springMVC 缓存(入门 spring+mybaties+redis一)
使用redis之前需要咋电脑上安装redis: 使用spring+mybaties+redis的本质是扩展类 org.apache.ibatis.cache.Cache:在我们自己扩展的Cache ...
- h5交互元素details标签
details是h5新增的交互元素,details与 summary 标签配合使用可以为 details 定义标题.默认情况下,不显示 details 标记中的内容.当用户点击标题时,会显示出 det ...
- netbeans tomcat
http://www.cnblogs.com/fengzy/archive/2013/05/18/3086371.html http://blog.csdn.net/xumengxing/articl ...
- ios 防止按钮快速点击造成多次响应的避免方法。
- (void)starButtonClicked:(id)sender { //先将未到时间执行前的任务取消. [[self class] cancelPreviousPerformRequests ...