85. Maximal Rectangle 由1拼出的最大矩形
[抄题]:
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
Example:
Input:
[
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
二为矩阵:2个0,一个null
[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
stack:把长度最长的列号暂存,然后取出来进行面积的比较
[一句话思路]:
新h[i]比旧h[i]长才能进,等于也行.
随着i的递增,旧h[i]比新h[i]长才用比较面积
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- h[]数组表示的是纵向长度,里面的index应该是横向坐标 cLen。多开辟一列 并且初始化为0,用于POP stack中之前的元素
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
stack中只存最之前和现在最长的
[复杂度]:Time complexity: O(n^2) Space complexity: O(n)
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
dp is 2 hard
[Follow Up]:
[LC给出的题目变变变]:
84 histogram
[代码风格] :
class Solution {
public int maximalRectangle(char[][] matrix) {
//cc
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
//ini: cLen, rLen, Stack : for longest index, h[rLen + 1]
int rLen = matrix.length, cLen = matrix[0].length, max = 0;
int[] h = new int[cLen + 1];
h[cLen] = 0;
//for loop: row (new stack) * col < cLen + 1
for (int row = 0; row < rLen; row++) {
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < cLen + 1; i++) {
//store h[i]
if (i < cLen)
if (matrix[row][i] == '1') h[i] += 1;
else h[i] = 0;
//store i, compare area, add i to stack again
if (stack.isEmpty() || h[i] >= h[stack.peek()])
//新比旧长才能进,等于也行
stack.push(i);
else {
while (!stack.isEmpty() && h[i] < h[stack.peek()]) {//旧比新长才用比较
int top = stack.pop();
int area = h[top] * (stack.isEmpty() ? i : i - stack.peek() - 1);
max = Math.max(max, area);
}
stack.push(i);
}
}
}
return max;
}
}
85. Maximal Rectangle 由1拼出的最大矩形的更多相关文章
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- [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. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- 85. Maximal Rectangle (Graph; Stack, DP)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 【LeetCode】85. Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
随机推荐
- 笔记:NPM 无限需要依赖问题解决
笔记:NPM 无限需要依赖问题解决 起因 因为想学一下 VUE,开始跟着教程一步一步输出命令,开始也没有什么问题,一切都很顺利. 突然不知道是哪一步出了问题,一直让我安装依赖,没完没了,开始并不觉得有 ...
- Regexper:牛逼的 JavaScript 正则可视化工具
RequireJS Optimizer 是 RequireJS 自带的前端优化工具,可以对 RequireJS 项目中的 JavaScript & CSS 代码使用 UglifyJS 或者 C ...
- yii2自定义500错误
由于项目想加预警监控,有一块儿是涉及到程序内部错误的500,这样的错误级别比较高,所以就需要捕获这样的错误,顺便自定义了一把视图样式 看了这篇博客,知道了如何去自定义自己错误页面 : http://t ...
- WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (一)
WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (一) 编写人:CC阿爸 2015-1-28 在伍华聪的博客中,看到布局控件"Weife ...
- nginx和php-fpm通信的两种方式 unix socket和TCP
nginx和fastcgi的通信方式有两种,一种是TCP 一种是unix socket TCP使用的是 127.0.0.1:9000端口,将fastcgi_pass参数修改为127.0.0.1:900 ...
- Oracle数据库clob字段导出为sql insert插入语句
oracle数据库的clob字段导出为sql insert插入语句可以分三种情况:1,clob没有换行符:2,clob有换行符但不以分号结尾:3,clob有换行符并且以分号结尾. clob没有换行符使 ...
- Java程序员之Spring(一) 入门
一. Spring 原理讲解 Spring 是一个轻量容器框架(开源):Spring的核心是 IoC(控制反转) 和 AOP(面向切面编程): Spring 由7个模块组成: Spring Core ...
- Java-Runoob-高级编程:Java 网络编程
ylbtech-Java-Runoob-高级编程:Java 网络编程 1.返回顶部 1. Java 网络编程 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. java.n ...
- nginx上传文件大小
采用nginx作反向代理,出现了一个诡异的问题,小文件可以提交,大文件会报500内部错误.这个是什么原因导致的呢? 查wiki可知,上传文件大小相关的有三个配置 client_body_buffer_ ...
- C++四种强制转换
C++的四种强制类型转换,所以C++不是类型安全的.分别为:static_cast , dynamic_cast , const_cast , reinterpret_cast 为什么使用C风格的强制 ...