【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 ...
随机推荐
- Yii2-admin RBAC权限管理的实现
原文地址:http://www.open-open.com/lib/view/open1434638805348.html http://wlzyan.blog.163.com/blog/stat ...
- webuploader横向按钮样式
#picker{display: inline-block;line-height: 1.428571429;vertical-align: middle;margin: 0 12px 0 0;wid ...
- WinForm中新开一个线程操作 窗体上的控件(跨线程操作控件)
最近在做一个winform的小软件(抢票的...).登录窗体要从远程web页面获取一些数据,为了不阻塞登录窗体的显示,开了一个线程去加载数据远程的数据,会报一个错误"线程间操作无效: 从不是 ...
- 【转载】Unity 合理安排增量更新(热更新)
原帖地址:由于我看到的那个网站发的这篇帖子很大可能是盗贴的,我就暂时不贴地址了.避免伤害原作者 原版写的有点乱,我个人修改整理了下. --------------------------------- ...
- 如何移除wordpress Admin Bar 上的 WordPress Logo
我们登陆wordpress后台在最上方会看到一些导航栏,默认会有WordPress Logo,如果你是“洁癖”肯定容不下这东西,那就折腾一下把它给消灭了 在当前主题的 functions.php插入如 ...
- 63.Hbase 常用命令
1.进入 Hbase shell ./hbase shell 2. 命令 1.list 2.truncate 3.scan 4.describe 5.create 'tablename','famil ...
- Swift2.1 语法指南——错误处理
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- 【转】 js怎么区分出点击的是鼠标左键还是右键?
IE 下 onMouseDown 事件有个 events.button 可以返回一个数值,根据数值判断取得用户按了那个鼠标键 events.button==0 默认.没有按任何按钮. events. ...
- CSU 1116 Kingdoms(枚举最小生成树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...
- Laravel 5.1 文档攻略 —— Eloquent: 读取器和修饰器
date_range 8月前 tag_faces Woody remove_red_eye 1483 chat0 简介 这一章其实很简单,Model的属性不是和数据表的字段一一对应吗? 那么在存储和呈 ...