Hard!

题目描述:

给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

示例:

  1. 输入:
  2. [
  3. ["1","0","1","0","0"],
  4. ["1","0","1","1","1"],
  5. ["1","1","1","1","1"],
  6. ["1","0","0","1","0"]
  7. ]
  8. 输出: 6

解题思路:

此题是之前那道的Largest Rectangle in Histogram 直方图中最大的矩形 (http://www.cnblogs.com/grandyang/p/4322653.html)的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用http://www.cnblogs.com/grandyang/p/4322653.html中的方法,就可以得到最大的矩形面积。那么这道题唯一要做的就是将每一层构成直方图,由于题目限定了输入矩阵的字符只有 '0' 和 '1' 两种,所以处理起来也相对简单。方法是,对于每一个点,如果是‘0’,则赋0,如果是 ‘1’,就赋 之前的height值加上1。

C++解法一:

  1. class Solution {
  2. public:
  3. int maximalRectangle(vector<vector<char> > &matrix) {
  4. int res = ;
  5. vector<int> height;
  6. for (int i = ; i < matrix.size(); ++i) {
  7. height.resize(matrix[i].size());
  8. for (int j = ; j < matrix[i].size(); ++j) {
  9. height[j] = matrix[i][j] == '' ? : ( + height[j]);
  10. }
  11. res = max(res, largestRectangleArea(height));
  12. }
  13. return res;
  14. }
  15. int largestRectangleArea(vector<int> &height) {
  16. int res = ;
  17. stack<int> s;
  18. height.push_back();
  19. for (int i = ; i < height.size(); ++i) {
  20. if (s.empty() || height[s.top()] <= height[i]) s.push(i);
  21. else {
  22. int tmp = s.top();
  23. s.pop();
  24. res = max(res, height[tmp] * (s.empty() ? i : (i - s.top() - )));
  25. --i;
  26. }
  27. }
  28. return res;
  29. }
  30. };

我们也可以在一个函数内完成,这样代码看起来更加简洁一些:

C++解法二:

  1. class Solution {
  2. public:
  3. int maximalRectangle(vector<vector<char>>& matrix) {
  4. if (matrix.empty() || matrix[].empty()) return ;
  5. int res = , m = matrix.size(), n = matrix[].size();
  6. vector<int> height(n + , );
  7. for (int i = ; i < m; ++i) {
  8. stack<int> s;
  9. for (int j = ; j < n + ; ++j) {
  10. if (j < n) {
  11. height[j] = matrix[i][j] == '' ? height[j] + : ;
  12. }
  13. while (!s.empty() && height[s.top()] >= height[j]) {
  14. int cur = s.top(); s.pop();
  15. res = max(res, height[cur] * (s.empty() ? j : (j - s.top() - )));
  16. }
  17. s.push(j);
  18. }
  19. }
  20. return res;
  21. }
  22. };

下面这种方法的思路很巧妙,height数组和上面一样,这里的left数组表示左边界是1的位置,right数组表示右边界是1的位置,那么对于任意一行的第j个位置,矩形为(right[j] - left[j]) * height[j],我们举个例子来说明,比如给定矩阵为:

  1. [
  2. [1, 1, 0, 0, 1],
  3. [0, 1, 0, 0, 1],
  4. [0, 0, 1, 1, 1],
  5. [0, 0, 1, 1, 1],
  6. [0, 0, 0, 0, 1]
  7. ]

第0行:

  1. h: 1 1 0 0 1
  2. l: 0 0 0 0 4
  3. r: 2 2 5 5 5

第1行:

  1. h: 1 1 0 0 1
  2. l: 0 0 0 0 4
  3. r: 2 2 5 5 5

第2行:

  1. h: 0 0 1 1 3
  2. l: 0 0 2 2 4
  3. r: 5 5 5 5 5

第3行:

  1. h: 0 0 2 2 4
  2. l: 0 0 2 2 4
  3. r: 5 5 5 5 5

第4行:

  1. h: 0 0 0 0 5
  2. l: 0 0 0 0 4
  3. r: 5 5 5 5 5

C++解法三:

  1. class Solution {
  2. public:
  3. int maximalRectangle(vector<vector<char>>& matrix) {
  4. if (matrix.empty() || matrix[].empty()) return ;
  5. int res = , m = matrix.size(), n = matrix[].size();
  6. vector<int> height(n, ), left(n, ), right(n, n);
  7. for (int i = ; i < m; ++i) {
  8. int cur_left = , cur_right = n;
  9. for (int j = ; j < n; ++j) {
  10. if (matrix[i][j] == '') ++height[j];
  11. else height[j] = ;
  12. }
  13. for (int j = ; j < n; ++j) {
  14. if (matrix[i][j] == '') left[j] = max(left[j], cur_left);
  15. else {left[j] = ; cur_left = j + ;}
  16. }
  17. for (int j = n - ; j >= ; --j) {
  18. if (matrix[i][j] == '') right[j] = min(right[j], cur_right);
  19. else {right[j] = n; cur_right = j;}
  20. }
  21. for (int j = ; j < n; ++j) {
  22. res = max(res, (right[j] - left[j]) * height[j]);
  23. }
  24. }
  25. return res;
  26. }
  27. };

我们也可以通过合并一些for循环,使得运算速度更快一些:

C++解法四:

  1. class Solution {
  2. public:
  3. int maximalRectangle(vector<vector<char>>& matrix) {
  4. if (matrix.empty() || matrix[].empty()) return ;
  5. int res = , m = matrix.size(), n = matrix[].size();
  6. vector<int> height(n, ), left(n, ), right(n, n);
  7. for (int i = ; i < m; ++i) {
  8. int cur_left = , cur_right = n;
  9. for (int j = ; j < n; ++j) {
  10. if (matrix[i][j] == '') {
  11. ++height[j];
  12. left[j] = max(left[j], cur_left);
  13. } else {
  14. height[j] = ;
  15. left[j] = ;
  16. cur_left = j + ;
  17. }
  18. }
  19. for (int j = n - ; j >= ; --j) {
  20. if (matrix[i][j] == '') {
  21. right[j] = min(right[j], cur_right);
  22. } else {
  23. right[j] = n;
  24. cur_right = j;
  25. }
  26. res = max(res, (right[j] - left[j]) * height[j]);
  27. }
  28. }
  29. return res;
  30. }
  31. };

LeetCode(85):最大矩形的更多相关文章

  1. Java实现 LeetCode 85 最大矩形

    85. 最大矩形 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 示例: 输入: [ ["1","0","1 ...

  2. leetcode 85. 最大矩形

    题目描述: 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 思路分析: 这题是之前那道最大正方形的进阶,同样是利用dp来求解.通过逐行去计算最大矩形,即优化的 ...

  3. LeetCode 85 | 如何从矩阵当中找到数字围成的最大矩形的面积?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题53篇文章,我们一起来看看LeetCode中的85题,Maximal Rectangle(最大面积矩形). 今天的 ...

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

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

  5. [LeetCode] 85. Maximal Rectangle 最大矩形

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

  6. 【LeetCode 85】最大矩形

    题目链接 [题解] 把所有的"1"矩形分成m类. 第j类的矩形.他的右边界跟第j列紧靠. 那么. 我们设f[i][j]表示(i,j)这个点往左最长能延伸多少个数目的"1& ...

  7. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

  8. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  9. [LeetCode] Rectangle Overlap 矩形重叠

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

  10. Leetcode 492. 构造矩形

    1.题目描述 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 1. 你设 ...

随机推荐

  1. struts2简单入门-参数传递的三种方式

    三种方式的简单说明 属性传递 把参数定义为属性提供get/set方法. 使用情况 参数少,不需要共享. 演示代码 public class LoginAction extends ActionSupp ...

  2. 工具方法 .js

    1. 获取url问号后面,name的值 /** * *?id=123&a=b * @return object */export function urlParse(){ let url = ...

  3. shell编程 之 流程控制(条件语句和循环语句)

    1 if ...else... 基本格式: if condition then commend else commend fi 当然也可以写到一行,用[ ]表明边界,用:表示分行.比如: if [ $ ...

  4. shell编程 之 test命令

    shell编程里的测试test命令基本可以分为3种数据类型,每种都不一样.个人更倾向于理解为条件语句的写法规则,就是test加条件加判断语句. 1 数值类型 基本可以分为6个判断:-eq等于,-ne不 ...

  5. 修复服务器上出现ImportError: cannot import name main的问题

    在服务器上成功升级pip2之后再运行pip2命令出现如下报错信息 Traceback (most recent call last): File "/usr/bin/pip2.7" ...

  6. Linux7系列阅读

    1.[Centos7]hostnamectl 设置主机名 https://blog.csdn.net/dream361/article/details/56833248 2.ip addr https ...

  7. CF1100F Ivan and Burgers

    题目地址:CF1100F Ivan and Burgers 一道有难度的线性基题,看了题解才会做 预处理两个数组: \(p_{r,i}\) 表示满足下列条件的最大的 \(l\) :线性基第 \(i\) ...

  8. 1.Python_字符串_常用办法总结

    明确:对字符串的操作方法都不会改变原来字符串的值. 1.去掉空格和特殊符号 name.strip() 去掉空格和换行符 name.strip("xx") 去掉某个字符串 name. ...

  9. python3+selenium框架设计03-封装日志类

    首先我们先来实现日志的功能,日志可以使用python3自带logging模块,不会的可以百度一下相关文章,也可以看我另外一篇文章Python3学习笔记24-logging模块 在封装日志类前,我们需要 ...

  10. Qt学习之路

      Qt学习之路_14(简易音乐播放器)   Qt学习之路_13(简易俄罗斯方块)   Qt学习之路_12(简易数据管理系统)   Qt学习之路_11(简易多文档编辑器)   Qt学习之路_10(Qt ...