[LeetCode] Maximal Rectangle(good)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
参考“Largest Rectangle in Histogram”这个题的解法,思想差不多一样,只是用h向量表示Rectangle中此元素中第一行到本行的高度,非常妙的算法:
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.size() == || matrix[].size() == )
return ;
int cLen = matrix[].size();
int rLen = matrix.size();
//height array
vector<int> h(cLen+,);
int max = ;
for(int row = ;row<rLen;row++){//for(1)
stack<int> s;
for(int i=;i<cLen+;i++){//for(2)
if(i<cLen){
if(matrix[row][i]=='')
h[i] += ;
else
h[i] = ;
}//end if
if(s.empty() || h[s.top()]<=h[i]){
s.push(i);
}else{
while(!s.empty() && h[i]<h[s.top()]){
int top = s.top();
s.pop();
int area = h[top]*(s.empty()?i:(i-s.top()-));
if(area > max)
max = area;
}//end while
s.push(i);
}//end if
}//end for(2)
}//end for(1)
return max;
}//end func
};
[LeetCode] Maximal Rectangle(good)的更多相关文章
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- leetcode之旅(11)-Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- LeetCode之旅(13)-Valid Anagram
题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- LeetCode高频题目(100)汇总-Java实现
LeetCode高频题目(100)汇总-Java实现 LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
- Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)
Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
随机推荐
- WPF之资源字典zz
最近在看wpf相关东西,虽然有过两年的wpf方面的开发经验,但是当时开发的时候,许多东西一知半解,至今都是模模糊糊,框架基本是别人搭建,自己也就照着模板写写,现在许多东西慢慢的理解了,回顾以前的若干记 ...
- get和post方法的区别
在form表单提交数据的过程中,method属性提供了两个值:get,post,默认为get方式[参1] 即一种为get提交,一种是post提交.那么这两种提交方式有什么不同呢? 查询了一些资料后,总 ...
- asp.net 微信企业号办公系统-流程设计--保存与发布
如果流程未设计完时可以先保存,以后再打开接着设计.点击工具栏上的保存按钮即可保存当前流程设计: 如果下次要接着设计,则可以打开该流程继续设计: 如果流程设计完成,可以点击安装按钮来发布流程,流程安装成 ...
- jquery easyui datagrid 分页详解
由于项目原因,用了jquery easyui 感觉界面不错,皮肤样式少点,可是官网最近打不开了,资料比较少,给的demo没有想要的效果,今天在用datagrid 做分页显示的时候,折腾了半天,网上的资 ...
- 网站建设中HTTP状态码的奥秘
在网络营销中,站长经常会遇到一些HTTP状态码的问题,不懂HTTP状态码那么做SEO优化就无从谈起,下面是脉凌网络对HTTP状态码总结的一览表. 1xx:请求收到,继续处理 2xx:操作成功收到,分析 ...
- Microsoft.AlphaImageLoader滤镜讲解
Microsoft.AlphaImageLoader是IE滤镜的一种,其主要作用就是对图片进行透明处理.虽然FireFox和IE7以上的IE浏览器已经支持透明的PNG图片,但是就IE5-IE6而言还是 ...
- jquery限制div字符长度,超出部分已“…”显示
$(".content").each(function(){ if($(this).text().length>100){ $(this).text($(this).text ...
- order by id asc得出的排序是什么原理
我们要用order by id asc得出的排序应该是,4,好了原理就这么简. sql实现方法,代码如下: : 代码如下: $sql ="Select 字段 from 表名 where id ...
- NBU7.0 Image Cleanup作业在没有配置hot catalog backup的情况下失败,Status=1
Issue NBU7.0 Image Cleanup作业在没有配置hot catalog backup的情况下失败,Status=1 Error NBU7.0 Image Cleanup作业失败, D ...
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...