[抄题]:

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]长才用比较面积

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 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拼出的最大矩形的更多相关文章

  1. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

  2. 刷题85. Maximal Rectangle

    一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...

  3. [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 ...

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

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

  5. leetcode[85] Maximal Rectangle

    给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...

  6. 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 ...

  7. 【LeetCode】85. Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  8. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

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

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

随机推荐

  1. Eclipse的Java开发中jar导入后无法使用包内class的解决方案

    请注意, 本方法只对于自己的包有效, 如果你的类内部互相调用, 此方法会失效, 需要每个类文件都进行一次CTRL+SHIFT+O进行包的导入. 如上图的一个结构, algs4.jar和stdlib.j ...

  2. vim初探

    https://github.com/spf13/spf13-vim 安装了此博主的开源项目. :vsp   ——竖分屏 :sp   ——横分屏

  3. windows平台最简单的rtmp/hls流媒体服务器

    feature: rtmp/hls live server for windows, double click to run,don't need config. run and quit: doub ...

  4. 第四章.使用ant编译hadoop eclipse插件

    从hadoop 0.20.203以后,hadoop的发布包里,不再对eclipse插件进行jar包发布,而是给出了打包的代码,需要各位开发人员自己进行打包和设置.我们打的包必须跟自己使用的hadoop ...

  5. 模拟admin组件自己开发stark组件之自定义list_display,反向解析url

    反向解析 在上一篇文章中,我们创建好了stark这个组件,一个应用一个表有四个默认的url,那么我们如何区别这些url,因为可能会有重复现象(本组件不会,因为前面拼接了应用名,表明,肯定唯一),概念请 ...

  6. ubuntu16.04让内核编译一次过的方法

    问题: 进入内核后,发现make menuconfig 出错,而且在在网上找到的一些安装包,安装结束后,发现make menuconfig后的图形界面虽然出来了,但是图形界面里的内容没有出来! 解决方 ...

  7. CentOS 7 需要安装的常用工具,及centos安装fcitx 搜狗输入法的坑旅

    https://blog.csdn.net/tham_/article/details/41868831 Centos常用设置 1.当最大化时隐藏标题栏 或者使用tweak tool 在字体中将标题栏 ...

  8. file_get_content服务器对服务器二进制文件上传

    1.file_get_contents函数可安全用于二进制对象,适用服务器对服务器文件是上传场景 base64_encode(file_get_contents('1268879774AaCl4wIE ...

  9. U盘启动安装WIN7(包含资源的地址)

    这几天在装win7和linux双系统,整理一下 第一种是在正常的windows下,网上下了镜像之后,装虚拟光驱,然后双击安装,按步骤执行即可,这个没什么好讲的. 第二种是windows坏掉,或者木有系 ...

  10. **请写出一段Python代码实现删除一个list里面的重复元素

    通常涉及到去重操作最好使用set,但是考虑到某些时候可能遇到不允许使用set的情况,那就自己实现一下: l = [2, 4, 5, 6, 1, 3, 4, 5] def f(l, b=0, c=1): ...