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

分析: 对于每一列从右到左看成一个直方图,每个直方图计算最大面积的时间复杂度为O(n) 所以总的时间复杂度是O(n2

class Solution {
public:
int maxArea(vector<int> &m)
{
int size = m.size();
stack<int> s;
int area, maxArea = ;
for(int i = ; i< size; ++i)
{
if(s.empty() || m[i] >= m[s.top()] )
{
s.push(i);
continue;
}
int tp = s.top();
s.pop();
area = m[tp] * (s.empty() ? i : i -s.top() -);
maxArea = maxArea > area ? maxArea : area;
--i;
} while(!s.empty()){
int tp = s.top();
s.pop();
area = m[tp] * (s.empty() ? size: size - s.top() -);
maxArea = maxArea > area ? maxArea : area;
}
return maxArea;
}
int maximalRectangle(vector<vector<char> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int row = matrix.size();
if(row < ) return ;
int column = matrix[].size();
if(column <) return ; int area , res = ; vector<int> m(row,);
for(int i = ; i< column; ++i)
{
for(int j = ; j< row; ++j)
if(matrix[j][i] == '')
m[j] = ;
else
m[j]++; area = maxArea(m);
res = res > area ? res : area;
}
return res;
}
};

LeetCode_Maximal Rectangle的更多相关文章

  1. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  2. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  3. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  4. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  5. [LeetCode] Maximal Rectangle 最大矩形

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

  6. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. Maximal Rectangle

    很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...

  8. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  9. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

随机推荐

  1. 【HDOJ】3316 Mine sweeping

    简单BFS. #include <iostream> #include <cstdio> #include <cstring> #include <cstdl ...

  2. uniq 命令

    uniq 命令 文字 uniq是LINUX命令 用途 报告或删除文件中重复的行. 语法 uniq [ -c | -d | -u ] [ -f Fields ] [ -s Characters ] [ ...

  3. [LeetCode] 128. Longest Consecutive Sequence 解题思路

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  4. MKMapView and Zoom Levels: A Visual Guide

    原帖:http://troybrant.net/blog/2010/01/mkmapview-and-zoom-levels-a-visual-guide/ So, how exactly does ...

  5. 左右TextView旋转门问题

    最近由于旋转门问题的一个客户找我,当在字符25更多的时候是不是走了,后来,我在重现的问题,发现问题如下面: 问题1.人物25几个月之内可以去. 问题2.在人物25个月,虽然比屏幕宽度,不去 问题3.屏 ...

  6. 设计模式(Java版)-创建型模式之简单工厂模式

    前言:这段时间在学习设计模式,本人也是小菜一枚(所以写的如果有错误的地方请大大们给予指出).这个东西也是我一直想学习的,从点点滴滴做起,记录下自己每天的领悟! 一.工厂模式的动机 在软件系统中,经常面 ...

  7. Aspose.Words导出dt到word的问题

    已解决:单挑数据导入到一个word文档,导出文字和图片成功,执行Main();方法导出dt到word 待解决:多条数据(文字,图片的导入到一个word文档里面)从dt导入到word;已发现的错误提示“ ...

  8. C#高级编程第1章-.NET体系结构

    内容提要: (1)编译和运行面向对象.NET代码 (2)IL/MSIL(Microsoft Intermediate Language)中间语言的优点 (3)值类型与引用类型 (4)数据类型化 (5) ...

  9. JS高级程序设计学习笔记之第三章基本概念(语法,数据类型,流控制语句,函数)——查漏补缺

    一.语法: 区分大小写; 2.标识符:就是指变量.函数.属性的名字,或者函数的参数 a.标志符的规则:①第一个字符必须是一个字母.下划线(_)或一个美元符号($).                   ...

  10. VS2010字体优化

    文本编辑器:Consolas 环境字体:微软雅黑