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 ...
随机推荐
- linux串口驱动分析——打开设备
串口驱动是由tty_driver架构实现的.一个应用程序中的函数要操作硬件,首先会经过tty,级级调用之后才会到达驱动之中.本文先介绍应用程序中打开设备的open函数的整个历程. 首先在串口初始化中会 ...
- [LeetCode] 179. Largest Number 解题思路
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- HDOJ 2191
多重背包. 模版. #include <iostream> #include <stdio.h> #include <stdlib.h> #include < ...
- C# 文件上传
一.分析 本次博客,主要解决文件上传等一系列问题,将从两方面来论述,即1G以内文件和1G以上文件. 对于上传1G以内的文件,可以采用基本的三种上传方法:用Web控件FileUpload.html控件 ...
- (转) xcodebuild和xcrun自动化编译ipa包 笔记
转自:http://blog.csdn.net/totogo2010/article/details/8883100 打包过程 xcodebuild负责将工程源文件编译成xxx.app xcrun负责 ...
- 关于phpmyadmin中添加外键的做法
今天想加个外键,又不想用命令行,打开PHPMYADMIN看怎么弄,找了半天没有找到添加外键的地方,然后上网搜了一会,发现目前的PHPMYADMIN确实没有这个设置,所以只能手动命令行添加了. 语法 ...
- 新建cocos2d-xproject
在cocos2d-x v3.1.1版本号中,採用命令行的方式来创建一个新的project.比方新建一个名为MyGame的project能够用以下的命令: watermark/2/text/aHR ...
- 常见算法:C语言求最小公倍数和最大公约数三种算法
最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们的公倍数,当中一个最小的公倍数是他们的最小公倍数,相同地,若干个整数公有的倍数中最小的正整数称为它们的最小公倍数,维基百科:定义点击打开链接 求 ...
- C#调用R语言输出图片
参考:http://rdotnet.codeplex.com/documentation REngine.SetEnvironmentVariables(); REngine engine = R ...
- Ubuntu网络管理
1.重启NetworkManager service network-manager stop rm /var/lib/NetworkManager/NetworkManager.state serv ...