题目
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area.
思路
对于上图的一个01矩阵。我们可以一行一行的分析,假设第三行,我们按列扫描,遇到0时,柱子断开,重新形成柱子,遇到1时柱子高度加一。这样的话,我们就可以把问题转换为[LeetCode]*84.Largest Rectangle in Histogram求解最大矩形面积。
代码
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int row = matrix.size();
if(row == 0){
return 0;
}
int col = matrix[0].size();
vector<vector<int> > height(row,vector<int>(col,0));
int h;
for(int j = 0;j < col;++j){
h = 0;
for(int i = 0;i < row;++i){
if(matrix[i][j] == '1'){
++h;
}
else{
h = 0;
}
height[i][j] = h;
}
}
int maxArea = 0;
for(int i = 0;i < row;++i){
maxArea = max(maxArea,MaxRectangle(height[i]));
}
return maxArea;
}
private:
int MaxRectangle(vector<int> height){
int size = height.size();
if(size == 0){
return 0;
}
int maxArea = 0;
stack<int> indexStack;
int top,width;
for(int i = 0;i < size;++i){
if(indexStack.empty() || height[i] >= height[indexStack.top()]){
indexStack.push(i);
}
else{
top = indexStack.top();
indexStack.pop();
width = indexStack.empty() ? i : (i - indexStack.top() - 1);
maxArea = max(maxArea,height[top] * width);
--i;
}
}
while(!indexStack.empty()){
top = indexStack.top();
indexStack.pop();
width = indexStack.empty() ? size : (size - indexStack.top() - 1);
maxArea = max(maxArea,height[top] * width);
}
return maxArea;
}
};
int main(){
Solution s;
vector<vector<char> > matrix = {
{'0','1','0','1','1'},
{'1','1','1','0','0'},
{'1','1','1','1','0'},
{'1','0','1','1','1'},
{'0','1','0','0','0'}
};
cout<<s.maximalRectangle(matrix)<<endl;
return 0;
}
运行时间
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。
JVM 基础 JAVA 并发 JVM 性能调优 LeetCode 算法 .......
LeetCode.370.区间加法器解法:差分数组assert nums.length > 0;// 构造差分数组public void increment(i
Leetcode Chrome extension..zip,Leetcode Chrome扩展。
My Solutions to Leetcode Database problems. 我的 Leetcode 数据.zip
用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现...
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...
LeetCode.swift.zip,很久以前,有一堆算法,他对斯威夫特略知一二。
LeetCode刷题题解.rar
leetcode - shuzu.html
LeetCode刷题手册.docx
LeetCode_book.rar
LeetCode-源码.rar
Leetcode-源码.rar
LeetCode-master.rar
leetcode-study.zip
玩转LeetCode算法题.pdf
谷歌高畅、BAT霜神leetcode刷题笔记.zip
好青年 _ leetcode 打卡群.zip