[抄题]:

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. linux主次设备号【转载】

    一个字符设备或者块设备都有一个主设备号和次设备号.主设备号和次设备号统称为设 备号.主设备号用来表示一个特定的驱动程序.次设备号用来表示使用该驱动程序的各 设备.例如一个嵌入式系统,有两个LED指示灯 ...

  2. php-fpm设置与 phpMyadmin超时 操作SQL超时

    LNMP 一键安装包环境: Phpmyadmin   登录超时 (1440 秒未活动),请重新登录. vim /usr/local/php/etc/php.ini session.gc_maxlife ...

  3. 优化html标签

    借用Effective之名,开始写Effective系列,总结一些前端的心得. 有些人写页面会走向一个极端,几乎页面所有的标签都用div,究其原因,用div有很多好处,一个是div没有默认样式,不会有 ...

  4. 解决div嵌套时IE8和FF无法自适应高度

    解决div嵌套时IE8和FF无法自适应高度 还是做类似新浪评论回复的时候,将回复的DIV嵌套在一个DIV中,然后点击回复的时候显示子DIV,这是父DIV的高度是会变化的,于是我将父DIV的高度设置为h ...

  5. StreamTool

    public class StreamTool { //从流中读取数据 public static byte[] read(InputStream inStream) throws Exception ...

  6. Bitcoin 涉及到的数据结构和算法分析

    Bitcoin 2008 年中本聪提出 Bitcoin 的概念. 2009 年项目上线. 所有 coin 由 mining 产生,一共 2100 万枚.通过调整 difficulty, 确保每隔10m ...

  7. databaseDesgin-temple

    ylbtech-dbs:ylbtech-storebook- A, 返回顶部 1, 2, B,返回顶部 1, 2, C,返回顶部 作者:ylbtech出处:http://storebook.cnblo ...

  8. 转:系统吞吐量(TPS)、用户并发量、性能测试概念和公式

    PS:下面是性能测试的主要概念和计算公式,记录下: 一.系统吞度量要素: 一个系统的吞度量(承压能力)与request对CPU的消耗.外部接口.IO等等紧密关联. 单个reqeust 对CPU消耗越高 ...

  9. 字体相关CSS属性介绍

    font-family 字体系列. font-family可以把多个字体名称作为一个“回退”系统来保存.如果浏览器不支持第一个字体,则会尝试下一个.浏览器会使用它可识别的第一个值. 简单实例: bod ...

  10. apt-get upgarde 和dist-upgrade的差别

    Debian/Ubuntu Linux都使用apt,升级时都是: apt-get update apt-get upgrade apt-get dist-upgrade 安装或升级系统分下面几个步骤. ...