[抄题]:

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) {//ccif (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 + 1for (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 againif (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;}
}

View Code

转载于:https://www.cnblogs.com/immiao0319/p/9062781.html

85. Maximal Rectangle 由1拼出的最大矩形相关推荐

  1. 【DP】LeetCode 85. Maximal Rectangle

    LeetCode 85. Maximal Rectangle Solution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk ...

  2. leetcode 85. Maximal Rectangle | 85. 最大矩形(单调栈)

    题目 https://leetcode.com/problems/maximal-rectangle/ 题解 本题与 leetcode 84. Largest Rectangle in Histogr ...

  3. [leetcode]@python 85. Maximal Rectangle

    题目链接 https://leetcode.com/problems/maximal-rectangle/ 题目原文 Given a 2D binary matrix filled with 0's ...

  4. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  5. LeetCode 85. Maximal Rectangle --python,java解法

    题目地址: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  6. 85. Maximal Rectangle

    用dp计算矩形面积 文章目录 1题目理解 2分析 2.1 暴力搜索 2.2 动态规划 3 相关题目 1题目理解 输入:char[][] matrix 是一个二维数组,值由0和1组成. 输出:一个矩形的 ...

  7. 85. Maximal Rectangle最大矩形

    艾恩凝 个人博客  https://aeneag.xyz/ 公众号 技术乱舞 每日一练,保持手感 2021/10/17 题目 https://leetcode-cn.com/problems/maxi ...

  8. [LeetCode]Maximal Rectangle

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

  9. [转载]在中文Windows环境下,控制台窗口中也可以用特殊符号拼出漂亮的表格来。...

    原文地址:在中文Windows环境下,控制台窗口中也可以用特殊符号拼出漂亮的表格来. 比如:             ┌─┬─┐     │  │作者:wxl1990721 在中文Windows环境下 ...

最新文章

  1. 记一次知乎维权过程——严肃批评某非法引流商
  2. QT的QDesignerFormWindowInterface类的使用
  3. linux 挂载ISO文件以及yum源配置
  4. KnockoutJS 3.X API 第七章 其他技术(2) 使用扩展器来增加可观察量(监控属性)
  5. 不知道路由器工作原理?没关系,来这看看!看不懂你捶我 | 原力计划
  6. 手机电脑壁纸!让你的桌面变得超酷
  7. 正版Fiddler下载地址
  8. web页面直接跳转至其他页面
  9. 2w字详解数据湖:概念、特征、架构与案例
  10. P,NP,NPC,NP-HARD 图片基于P!=NP
  11. 51nod 1076 2条不相交的路径 无向图强联通分量 trajan算法
  12. 模拟器连接本地服务器
  13. [Unity3D]Unity3D游戏开发之从《魂斗罗》游戏说起(上)——目标追踪
  14. 苹果手机用什么蓝牙耳机好?适合苹果的音乐蓝牙耳机推荐
  15. java爬虫实战——实现简单的爬取网页数据
  16. DorisDB系列文档(三)基本原理
  17. 【Android 电量优化】电量优化 ( 唤醒锁定 | 使用 WeakLock 保持服务唤醒 | 屏幕唤醒 )
  18. python(pyqt5)多线程解决界面无响应
  19. oracle中怎么判断为周五,求一年中所有星期五的日期
  20. java 转byte字符串,Java 字符串与byte之间的相互转换

热门文章

  1. 202012没有oracle加密,Windows2012远程桌面提示"这可能是由于 CredSSP 加密 Oracle 修正" 修复方法...
  2. [蓝桥杯]试题 基础练习 Huffuman树
  3. 文件包含原理及本地文件包含漏洞演示(本地文件,远程包含文件的测试)
  4. [GCN] 代码解析 of GitHub:Semi-supervised classification with graph convolutional networks
  5. [深度学习] 池化层函数及其逆过程函数
  6. java使用poi读取存储excel表格,包括xls和xlsx格式
  7. Perceptual Losses for Real-Time Style Transfer and Super-Resolution
  8. 频率分布直方图组距如何确定_频率分布有关的概念
  9. [codewars] - int32 to IPv4 二进制十进制 ip地址转换
  10. Jmeter性能测试实战教程系列-搭建分布式性能测试环境(五)