Question

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

Solution

这个题目可以借鉴LeetCode——largest-rectangle-in-histogram 的思路,求最大矩阵的面积,所以我们只需要按行把给定’0’,‘1’矩阵,转换成对应的高度即可,然后计算最大矩阵面积。

Code

时间复杂度O(n^2).

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>> heights;
for (int i = 0; i < row; i++) {
vector<int> tmp;
for (int j = 0; j < col; j++) {
if (matrix[i][j] != '0') {
// 累加高度
if (i - 1 >= 0 && matrix[i - 1][j] != '0')
matrix[i][j] += (matrix[i - 1][j] - '0');
tmp.push_back(matrix[i][j] - '0');
} else {
// 被‘0’断开了,就需要计算一次
if (tmp.size() > 0) {
heights.push_back(tmp);
tmp.clear();
}
}
}
if (tmp.size() > 0)
heights.push_back(tmp);
}
int res = 0;
for (int i = 0; i < heights.size(); i++) {
res = max(res, calc(heights[i]));
}
return res;
}
// 给定高度,求最大矩阵面积,时间复杂度O(n)
int calc(vector<int>& height) {
if (height.size() <= 0)
return 0;
stack<int> tb;
int n = height.size();
int res = 0;
for (int i = 0; i < n; i++) {
while (!tb.empty() && height[tb.top()] >= height[i]) {
int index = tb.top();
tb.pop();
if (tb.empty()) {
res = max(res, i * height[index]);
} else {
res = max(res, (i - tb.top() - 1) * height[index]);
}
}
tb.push(i);
}
while (!tb.empty()) {
int index = tb.top();
tb.pop();
if (tb.empty()) {
res = max(res, n * height[index]);
} else {
res = max(res, (n - tb.top() - 1) * height[index]);
}
}
return res;
}
};

LeetCode——maximal-rectangle的更多相关文章

  1. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  2. LeetCode: Maximal Rectangle 解题报告

    Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...

  3. [LeetCode] Maximal Rectangle 最大矩形

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

  4. [leetcode]Maximal Rectangle @ Python

    原题地址:https://oj.leetcode.com/problems/maximal-rectangle/ 题意:Given a 2D binary matrix filled with 0's ...

  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] Maximal Rectangle(good)

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

  7. leetcode -- Maximal Rectangle TODO O(N)

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

  8. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  9. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  10. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

随机推荐

  1. [iOS微博项目 - 4.2] - 设置转发微博背景

    github: https://github.com/hellovoidworld/HVWWeibo A.转发微博部分的淡灰色背景 1.需求 转发微博部分需要设置背景色 使用图片作为背景   2.思路 ...

  2. 小程序 当button遇上Flex布局

    当需要将button按行排列,当超过一行时,可以换行,从左到右排列,想实现如下效果(实现的比较粗糙,能说明问题就行,呵~~~): 使用Flex布局,在设置主轴方向上对齐方式,使用justify-con ...

  3. redisTemplate写哈希表遇到的坑

    本文系原创,如有转载,请注明出处 在使用spring的redisTemplate进行redis哈希表的相关操作时,遇到了下面比较奇怪的情况: 1.删掉哈希表所属的key之后,重新get这个key的值, ...

  4. mac配置python自然语言处理环境

    一.nltk安装 Ⅰ.工具安装步骤 1.根据python版本从 https://pypi.python.org/pypi/setuptools 下载对应版本的setuptools.然后,在终端下运行, ...

  5. qemu网络虚拟化之数据流向分析二

    2016-09-27 上篇文章大致介绍了qemu网络虚拟化相关的数据结构,本篇就结合qemu-kvm源代码分析下各个数据结构是如何初始化以及建立联系的. 这里还是分为三个部分: 1.Tap设备区 2. ...

  6. git mv与直接mv的区别

    git mv 行为: 1.创建一个和之前文件内容一样的文件,文件名为新的文件名 2.将原来的文件删除 3.将删除的文件添加到暂存区 4.将新建的文件添加到暂存区 $ git mv a a1 $ git ...

  7. UVA10700:Camel trading(栈和队列)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/J 题目大意: 给一个没有加上括号的表达式且只有+ , ...

  8. HDU5003:Osu!(签到题)HDU5038:(签到题,题意很坑)

    HDU 5003 水题,直接上代码(因为题意读错了,WA了一遍). #include <iostream> #include <string.h> #include <s ...

  9. 这样才能使本地Mysql服务允许被外部主机连接(两步)

    网上的N多方法都不全面,只有下面的第一步或第二步是不行的,必须同时执行下面两步操作: 修改mysql.user表 以root或debian-sys-maint身份登录mysql $ mysql -u ...

  10. android整理的一些基础知识

    本篇文章内容大部分是来源于本人实际开发中的心得总结,不是非常全面,咱才疏学浅,如果有错误的地方恳请各位指出哦~ android四大组件 四大组件包括:Activity(活动),Service(服务), ...