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的时候有人说可以用在这题,看了一下还真是,以每行为x轴,每列往上累计的连续的1当成高度,就可以完全使用一样的方法了。

 int largestArea(vector<int>height){
stack<int> s;
int maxArea = ;
int i = ;
height.push_back();
int len = height.size(); while (i < len){
if (s.empty() || height[s.top()] < height[i]){
s.push(i++);
}else{
int t = s.top();
s.pop();
maxArea = max(maxArea, height[t] * (s.empty()? i : (i-s.top()-)));
}
}
return maxArea;
} int maximalRectangle(vector<vector<char>> &matrix){
vector<int> height;
int maxRect=;
for (int row=; row<matrix.size(); row++){
for (int col=; col<matrix[row].size(); col++){
if (matrix[row][col] == ''){
height.push_back();
}
else{
int c=;
for (int i=row; i>-; i--){
if (matrix[i][col] != ''){
c++;
}else {
break;
}
}
height.push_back(c);
}
} for (int i=;i<height.size(); i++){
cout << height[i] << " ";
}
cout << endl; maxRect = max(maxRect, largestArea(height));
height.clear();
}
return maxRect;
} int maximalRectangle2(vector<vector<char>> &matrix){
int maxRect = ;
if (matrix.size() < ) return ;
vector<int>height(matrix[].size(), );
for (int i=; i<matrix.size(); i++){
for (int j=; j<matrix[i].size(); j++){
if (matrix[i][j] == ''){
height[j] += ;
}else{
height[j] = ;
}
}
maxRect = max(maxRect, largestArea(height));
}
return maxRect;
}

第一个maximalRectangle函数我用了很蠢的遍历方法来获得每行的height,这样活生生的把原本O(n^2)搞成了O(n^3)。。。 最后看了别人博客,说height是可以通过前一行的值算出了的(有点类似DP的思想...如果这也算的话),豁然开朗,才写出了

maximalRectangle2这个真正的O(n^2)方法。

[LeetCode] Maximal Rectangle的更多相关文章

  1. leetcode Maximal Rectangle 单调栈

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

  2. LeetCode: Maximal Rectangle 解题报告

    Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...

  3. [LeetCode] Maximal Rectangle 最大矩形

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

  4. [leetcode]Maximal Rectangle @ Python

    原题地址:https://oj.leetcode.com/problems/maximal-rectangle/ 题意:Given a 2D binary matrix filled with 0's ...

  5. [LeetCode] Maximal Rectangle(good)

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

  6. leetcode -- Maximal Rectangle TODO O(N)

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

  7. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  8. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  9. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

随机推荐

  1. Codeforces 727 F. Polycarp's problems

    Description 有一个长度为 \(n\) 有正负权值的序列,你一开始有一个值,每次到一个权值就加上,最少需要删掉多少数值才能到序列末尾.\(n \leqslant 750,m \leqslan ...

  2. jquery为新增元素添加事件

    <script type="text/javascript"> var $id=1; $(function(){ $(".hehe").click( ...

  3. CSS——display和float

    1.display 属性规定元素应该生成的框的类型. 值 描述 none 此元素不会被显示. block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会被显示为内联元素 ...

  4. JavaScript——this、constructor、prototype

    this this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window: 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用. 我们还可以使用a ...

  5. 【Unity3D】计算二维向量夹角(-180到180)

    在Unity3D中,有时候我们需要计算二维向量的夹角.二维向量夹角一般在0~180度之前,可以直接调用Vector2.Angle(Vector2 from, Vector2 to)来计算. 但是在有些 ...

  6. 新建samba配置步骤

    Linux系统默认已经安装了Samba,但是没有安装Samba服务: 1,先查看安装情况:rpm -qa|grep samba 根据系统的安装情况选择下载或者通过光驱安装所缺的rpm包. 我的安装情况 ...

  7. 使用jsvc启动tomcat

    1.在/usr/local/apache-tomcat-7.0.68/bin中有commons-daemon-native.tar.gz  压缩包 2.解压commons-daemon-native. ...

  8. cf515d

    题意:给出一个矩阵迷宫,要求用1×2的积木填满空白区域,问解法是否唯一,如果无解或者多解均输出“Not unique". 分析:广搜.看似二分图匹配但实际上不是. 我们认为每个点和上下左右四 ...

  9. MongoDB 副本集的相关概念【转】

    一.副本集基本概念 副本集(replica set) MongoDB的replica set是一个mongod进程实例簇,数据在这个簇中相互复制,并自动进行故障切换. MongoDB的数据库复制增加了 ...

  10. Windows 下的 MarkdownPad 2 工具使用

    MarkdownPad 2 工具(windows) 一. 软件下载和安装 下载登陆官网: http://markdownpad.com/ 点击Download,会自动下载.或者直接点击http://m ...