LeetCode_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.
分析: 对于每一列从右到左看成一个直方图,每个直方图计算最大面积的时间复杂度为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的更多相关文章
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [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 ...
- [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 ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- Maximal Rectangle
很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
随机推荐
- BZOJ3296: [USACO2011 Open] Learning Languages
3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 81 Solved: ...
- HDU_1238——最大子串搜索
Problem Description You are given a number of case-sensitive strings of alphabetic characters, find ...
- 学习DNS路上之CloudXNS
使用CloudXNS已经有两年了,趁着他们现在做活动的机会也发表一下这两年来使用感受与CloudXNS的变化,也对我学习使用的一次总结. 简介 CloudXNS是北京快网开发的一套授权DNS系统, 用 ...
- SQL - 配置SQLServer 使其可以远程访问
环境: SQL Server2008 R2 SQL Server Management Studio 今天测试部署项目的时候,发现不能远程访问SQL Server.具体情形就是在Management ...
- maven 命令小记
mvn help:system mvn clean compile mvn clean test 测试 mvn clean package ...
- (原)Apache添加完限速模块后的文件
点我下载 解压后得到apache2文件夹和readme.txt文本 按照readme.txt修改apache2文件夹.
- sql server 2008 在安装了活动目录以后无法启动服务了
软件环境: windows server 2008 r2 ms sql server 2008 r2 在安装活动目录以前,数据库是正常运行的. 安装了活动目录以后,数据库启动时就提示无法启动.出错的信 ...
- HBase总结(十二)Java API 与HBase交互实例
HBase提供了Java Api的訪问接口,掌握这个就跟Java应用使用RDBMS时须要JDBC一样重要 import java.io.IOException; import org.apache.h ...
- Java和C#中String直接赋值与使用new创建(==与equals进行比较)的区别
在Java中,字符串可以直接赋值或者使用new来新建,直接赋值的话是编译阶段(.class文件)中就将该字符串值放到常量池中,以后如果有其他变量直接赋予同样的值的话就不再分配内存空间,而是直接给它个引 ...
- Eclipse 添加快捷方式
1.在/usr/share/applications创建一个desktop文件,命名为eclipse.desktop 文件内容如下 [Desktop Entry]Name=EclipseType=Ap ...