LeetCode(85):最大矩形
Hard!
题目描述:
给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
示例:
- 输入:
- [
- ["1","0","1","0","0"],
- ["1","0","1","1","1"],
- ["1","1","1","1","1"],
- ["1","0","0","1","0"]
- ]
- 输出: 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++解法一:
- class Solution {
- public:
- int maximalRectangle(vector<vector<char> > &matrix) {
- int res = ;
- vector<int> height;
- for (int i = ; i < matrix.size(); ++i) {
- height.resize(matrix[i].size());
- for (int j = ; j < matrix[i].size(); ++j) {
- height[j] = matrix[i][j] == '' ? : ( + height[j]);
- }
- res = max(res, largestRectangleArea(height));
- }
- return res;
- }
- int largestRectangleArea(vector<int> &height) {
- int res = ;
- stack<int> s;
- height.push_back();
- for (int i = ; i < height.size(); ++i) {
- if (s.empty() || height[s.top()] <= height[i]) s.push(i);
- else {
- int tmp = s.top();
- s.pop();
- res = max(res, height[tmp] * (s.empty() ? i : (i - s.top() - )));
- --i;
- }
- }
- return res;
- }
- };
我们也可以在一个函数内完成,这样代码看起来更加简洁一些:
C++解法二:
- class Solution {
- public:
- int maximalRectangle(vector<vector<char>>& matrix) {
- if (matrix.empty() || matrix[].empty()) return ;
- int res = , m = matrix.size(), n = matrix[].size();
- vector<int> height(n + , );
- for (int i = ; i < m; ++i) {
- stack<int> s;
- for (int j = ; j < n + ; ++j) {
- if (j < n) {
- height[j] = matrix[i][j] == '' ? height[j] + : ;
- }
- while (!s.empty() && height[s.top()] >= height[j]) {
- int cur = s.top(); s.pop();
- res = max(res, height[cur] * (s.empty() ? j : (j - s.top() - )));
- }
- s.push(j);
- }
- }
- return res;
- }
- };
下面这种方法的思路很巧妙,height数组和上面一样,这里的left数组表示左边界是1的位置,right数组表示右边界是1的位置,那么对于任意一行的第j个位置,矩形为(right[j] - left[j]) * height[j],我们举个例子来说明,比如给定矩阵为:
[
[1, 1, 0, 0, 1],
[0, 1, 0, 0, 1],
[0, 0, 1, 1, 1],
[0, 0, 1, 1, 1],
[0, 0, 0, 0, 1]
]
第0行:
- h: 1 1 0 0 1
- l: 0 0 0 0 4
- r: 2 2 5 5 5
第1行:
- h: 1 1 0 0 1
- l: 0 0 0 0 4
- r: 2 2 5 5 5
第2行:
- h: 0 0 1 1 3
- l: 0 0 2 2 4
- r: 5 5 5 5 5
第3行:
- h: 0 0 2 2 4
- l: 0 0 2 2 4
- r: 5 5 5 5 5
第4行:
- h: 0 0 0 0 5
- l: 0 0 0 0 4
- r: 5 5 5 5 5
C++解法三:
- class Solution {
- public:
- int maximalRectangle(vector<vector<char>>& matrix) {
- if (matrix.empty() || matrix[].empty()) return ;
- int res = , m = matrix.size(), n = matrix[].size();
- vector<int> height(n, ), left(n, ), right(n, n);
- for (int i = ; i < m; ++i) {
- int cur_left = , cur_right = n;
- for (int j = ; j < n; ++j) {
- if (matrix[i][j] == '') ++height[j];
- else height[j] = ;
- }
- for (int j = ; j < n; ++j) {
- if (matrix[i][j] == '') left[j] = max(left[j], cur_left);
- else {left[j] = ; cur_left = j + ;}
- }
- for (int j = n - ; j >= ; --j) {
- if (matrix[i][j] == '') right[j] = min(right[j], cur_right);
- else {right[j] = n; cur_right = j;}
- }
- for (int j = ; j < n; ++j) {
- res = max(res, (right[j] - left[j]) * height[j]);
- }
- }
- return res;
- }
- };
我们也可以通过合并一些for循环,使得运算速度更快一些:
C++解法四:
- class Solution {
- public:
- int maximalRectangle(vector<vector<char>>& matrix) {
- if (matrix.empty() || matrix[].empty()) return ;
- int res = , m = matrix.size(), n = matrix[].size();
- vector<int> height(n, ), left(n, ), right(n, n);
- for (int i = ; i < m; ++i) {
- int cur_left = , cur_right = n;
- for (int j = ; j < n; ++j) {
- if (matrix[i][j] == '') {
- ++height[j];
- left[j] = max(left[j], cur_left);
- } else {
- height[j] = ;
- left[j] = ;
- cur_left = j + ;
- }
- }
- for (int j = n - ; j >= ; --j) {
- if (matrix[i][j] == '') {
- right[j] = min(right[j], cur_right);
- } else {
- right[j] = n;
- cur_right = j;
- }
- res = max(res, (right[j] - left[j]) * height[j]);
- }
- }
- return res;
- }
- };
LeetCode(85):最大矩形的更多相关文章
- Java实现 LeetCode 85 最大矩形
85. 最大矩形 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 示例: 输入: [ ["1","0","1 ...
- leetcode 85. 最大矩形
题目描述: 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 思路分析: 这题是之前那道最大正方形的进阶,同样是利用dp来求解.通过逐行去计算最大矩形,即优化的 ...
- LeetCode 85 | 如何从矩阵当中找到数字围成的最大矩形的面积?
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题53篇文章,我们一起来看看LeetCode中的85题,Maximal Rectangle(最大面积矩形). 今天的 ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- [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 ...
- 【LeetCode 85】最大矩形
题目链接 [题解] 把所有的"1"矩形分成m类. 第j类的矩形.他的右边界跟第j列紧靠. 那么. 我们设f[i][j]表示(i,j)这个点往左最长能延伸多少个数目的"1& ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Rectangle Overlap 矩形重叠
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
- Leetcode 492. 构造矩形
1.题目描述 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 1. 你设 ...
随机推荐
- struts2简单入门-参数传递的三种方式
三种方式的简单说明 属性传递 把参数定义为属性提供get/set方法. 使用情况 参数少,不需要共享. 演示代码 public class LoginAction extends ActionSupp ...
- 工具方法 .js
1. 获取url问号后面,name的值 /** * *?id=123&a=b * @return object */export function urlParse(){ let url = ...
- shell编程 之 流程控制(条件语句和循环语句)
1 if ...else... 基本格式: if condition then commend else commend fi 当然也可以写到一行,用[ ]表明边界,用:表示分行.比如: if [ $ ...
- shell编程 之 test命令
shell编程里的测试test命令基本可以分为3种数据类型,每种都不一样.个人更倾向于理解为条件语句的写法规则,就是test加条件加判断语句. 1 数值类型 基本可以分为6个判断:-eq等于,-ne不 ...
- 修复服务器上出现ImportError: cannot import name main的问题
在服务器上成功升级pip2之后再运行pip2命令出现如下报错信息 Traceback (most recent call last): File "/usr/bin/pip2.7" ...
- Linux7系列阅读
1.[Centos7]hostnamectl 设置主机名 https://blog.csdn.net/dream361/article/details/56833248 2.ip addr https ...
- CF1100F Ivan and Burgers
题目地址:CF1100F Ivan and Burgers 一道有难度的线性基题,看了题解才会做 预处理两个数组: \(p_{r,i}\) 表示满足下列条件的最大的 \(l\) :线性基第 \(i\) ...
- 1.Python_字符串_常用办法总结
明确:对字符串的操作方法都不会改变原来字符串的值. 1.去掉空格和特殊符号 name.strip() 去掉空格和换行符 name.strip("xx") 去掉某个字符串 name. ...
- python3+selenium框架设计03-封装日志类
首先我们先来实现日志的功能,日志可以使用python3自带logging模块,不会的可以百度一下相关文章,也可以看我另外一篇文章Python3学习笔记24-logging模块 在封装日志类前,我们需要 ...
- Qt学习之路
Qt学习之路_14(简易音乐播放器) Qt学习之路_13(简易俄罗斯方块) Qt学习之路_12(简易数据管理系统) Qt学习之路_11(简易多文档编辑器) Qt学习之路_10(Qt ...