[抄题]:

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. Android照片墙完整版,完美结合LruCache和DiskLruCache

    转载地址:http://blog.csdn.net/guolin_blog/article/details/34093441#comments 在上一篇文章当中,我们学习了DiskLruCache的概 ...

  2. 如何在 Linux 上使用 x2go 设置远程桌面

    https://linux.cn/article-5708-1.html 由于一切都迁移到了云上,作为提高职员生产力的一种方式,虚拟远程桌面在工业中越来越流行.尤其对于那些需要在多个地方和设备之间不停 ...

  3. Oracle存储过程记录异常日志

    一般我们会将一些涉及到数据库的定时任务直接用存储过程搞定,省去了后端代码的开发.部署,简单.快速,但这种方式存在一个弊端——当存储过程执行出错了,我们无法感知.解决办法也简单,学代码那样去捕获异常.打 ...

  4. RK3288 usb 摄像头旋转

    系统:Android 5.1 下面实现了摄像头 180 度旋转,旋转角度只需修改 orientation. diff --git a/hardware/rockchip/camera/CameraHa ...

  5. 机器学习(Machine Learning)&深度学习(Deep Learning)资料(下)

    转载:http://www.jianshu.com/p/b73b6953e849 该资源的github地址:Qix <Statistical foundations of machine lea ...

  6. css怎么设置2个div同行,第一个固定宽度,第二个占满剩余的部分

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 【转】JMeter技巧集锦

    JMeter是一个流行的用于负载测试的开源工具,具有许多有用的功能元件,如线程组(threadgroup),定时器(timer),和HTTP取样(sampler)元件.本文是对JMeter用户手册的补 ...

  8. s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由用户控制。

    package com.hanqi; import java.util.*; public class yonghukongzhi { public static void main(String[] ...

  9. 20181106_线程之异常_取消_变量_安全Lock

    一. 线程的异常处理: try { TaskFactory taskFactory = new TaskFactory(); List<Task> taskList = new List& ...

  10. Visual Studio Online 创建项目

    VSO是微软为软件开发人员提供的一款基于云计算的开发平台.Team Foundation Server已经可以基于云端使用,无需再为配置和部署耗费多余的时间(PS:当初为了在服务器上部署这个鼓捣了4个 ...