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. Gartner提出的7种多租户模型

    下面,我们就来看看在SaaS应用搭建过程中,可以采用什么样的多租户模型.从而能较为清晰地了解未来使用PaaS平台开发的SaaS,可以为用户提供哪些多租户的服务.        Gartner提出了7种 ...

  2. linux创建lvm分区

    创建LVM分区 shell> fdisk /dev/xvdb #### 选择磁盘 Command (m for help): m #### 帮助 Command action a toggle ...

  3. NoSQL文章

    MongoDB Bugsnag的MongoDB分片集群使用经验

  4. python 将日期戳(五位数时间)转换为标准时间

    5位数日期戳 读取 .mat 文件处理里面数据时,发现里面的日期数据全部都是 “5位数” 数字,很不解: 后来查到可以在excel中通过设置单元格调回标准日期格式,如下: 选中日期戳,右键选择 “格式 ...

  5. git学习------>git-rev-parse命令初识

    一.准备工作 第一步:在d盘git test目录下,新建工作区根目录demo,进入该目录后,执行git init创建版本库. DH207891+OuyangPeng@DH207891 MINGW32 ...

  6. 登录plsql 报错 the account is locked --用户被锁

    登录数据库服务器,进入oracle用户下: [root@uumsnormal-oracle admin]# su - oracle [oracle@uumsnormal-oracle ~]$ sqlp ...

  7. 磁钉导航差速式AGV控制实验

    磁钉导航AGV实验 2016-03 本机器是采用RFID电子地标配合磁钉传感器的定位导航AGV.本AGV已初步实现里程计精确解算,磁钉数据融合,AGV定点精准停车.原地旋转换向.远程无线调度的功能,初 ...

  8. Appium-Java滑动操作

    Java滑动操作,通常可以直接使用API中AndroidDriver类中的swipe方法,直接进行调用 swipe(int startx, int starty, int endx, int endy ...

  9. [转]linux shell 获取当前正在执行脚本的绝对路径

    原文链接:http://sexywp.com/bash-how-to-get-the-basepath-of-current-running-script.htm 常见的一种误区,是使用 pwd 命令 ...

  10. Java泛型三:Java泛型详解

    原文地址https://www.cnblogs.com/lzq198754/p/5780426.html 1.为什么需要泛型 泛型在Java中有很重要的地位,网上很多文章罗列各种理论,不便于理解,本篇 ...