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)的更多相关文章

  1. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  2. leetcode之旅(11)-Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  3. LeetCode之旅(13)-Valid Anagram

    题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  4. LeetCode高频题目(100)汇总-Java实现

    LeetCode高频题目(100)汇总-Java实现       LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...

  5. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  6. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  7. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

  8. Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)

    Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...

  9. Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)

    Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...

随机推荐

  1. okhttp3 get post 简单封装

    最近打算在新项目中使用 okhttp3, 简单封装了一下异步 get post 因为 CallBack 也是在子线程中执行,所以用到了 Handler public class MyOkHttpCli ...

  2. BZOJ2862 : 分糖果

    二分答案$x$表示最大的一段的和. 设$f[i]$表示前$i$个最多分几段,满足最大的一段不超过$x$,若$f[n]\geq k$,则可行, 则$f[i]=\max(f[j])+1,sum[i]-su ...

  3. CC150 - 11.5

    Question: Given a sorted array of strings which is interspersed with empty strings, write a method t ...

  4. pygame系列_百度随心听_完美的UI设计

    这个程序的灵感来自于百度随心听 下面是我的程序截图: 说明: 动作按钮全部是画出来的,没有用到任何图片 用到图片的只有:背景,歌手图片,作者图片 代码正在调试中.... 如果你鼠标移动到黄色小圆里面, ...

  5. java实现吸血鬼数字

    public class Vempire { public static void main(String[] arg) { String[] ar_str1, ar_str2; ; int from ...

  6. WPF DataGrid的分页实现

    原理:其实分页功能的实现大家都清楚,无非就是把一个记录集通过运算来刷选里面对应页码的记录. 接来下我们再次添加新的代码 <Grid> <DataGrid  Name="da ...

  7. SQL - 分页存储过程

    http://www.jb51.net/article/71193.htm http://www.webdiyer.com/utils/spgenerator/ create PROCEDURE [d ...

  8. 1. Nginx

    参考: http://www.darrenfang.com/2014/02/install-php-with-nginx-on-ubuntu/   安装: apt-get install python ...

  9. replication set复制集

    replication set复制集  介绍 replicattion set 多台服务器维护相同的数据副本,提高服务器的可用性,总结下来有以下好处: 数据备份与恢复 读写分离 MongoDB 复制集 ...

  10. Codeforces Round #360 (Div. 2) C D E

    每次AB秒出 到了C难度陡然上升...翻译都弄不懂... C 给出一张图 找出两个点的覆盖集(覆盖集是指这图中每条边都有至少一个点在这个点集里面) 并且两个点集没有交集 英文很难看懂...就是二分图的 ...