LeetCode_Maximal Rectangle
2024-10-29 15:33:52
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;
}
};
最新文章
- Python开发【第二篇】:Python基础知识
- java1234教程系列笔记 S1 Java SE 02 eclipse初步使用、注释、标识符
- 手机GPS为什么能在室内定位?
- C++11 并发指南六( <;atomic>; 类型详解二 std::atomic )
- 开发(ASP.NET程序)把写代码写至最有面向对象味道
- WINCE6.0组件选择说明
- Machine Learning – 第2周(Linear Regression with Multiple Variables、Octave/Matlab Tutorial)
- OpenCV码源笔记——RandomTrees (二)(Forest)
- jsp页面使用javascript添加页面元素示例代码
- StartCoroutine/StopCoroutineInvoke
- DBA 经典面试题(1)
- HDU 2510 - 符号三角形
- 开源企业IM-免费企业即时通讯-ENTBOOST V2014.180 Linux版本号正式公布
- 选择一个利于SEO的空间
- Windows和Linux如何使用Java代码实现关闭进程
- MongoDb进阶实践之三 Mongodb基本命令详解
- va_start
- 爬虫 selenium+Xpath 爬取动态js页面元素内容
- 自然语言推断(NLI)、文本相似度相关开源项目推荐(Pytorch 实现)
- eclipse中git推送上传错误 没有足够的数据写入