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

给出一个二维的char数组,判断其中‘1’构成的最大矩形面积

注意两点:1,input是char类型数组,而不是int , 2.矩形

思路:
两种方法,一种是84题的拓展,一种是dp

(1) 84题的拓展
一层一层读input,把每个元素看成直方图的高度,用一个数组heights[]保存累加高度
第一行: heights={1, 0, 1, 0, 0}
第二行:heights变成{2, 0, 2, 1, 1}

即元素=0时,heights对应的元素清0, =1时heights对应元素+1

把heights看作是一个直方图,这时转化为求直方图中最大矩形面积
每一行设置完heights后,都求一次最大矩形面积

其中maxHistArea函数为求直方图最大矩阵面积函数,参照84题代码

    public int maximalRectangle(char[][] matrix) {if(matrix == null || matrix.length == 0) {return 0;}int m = matrix.length;int n = matrix[0].length;int[] height = new int[n];int result = 0;for(int i = 0; i < m; i++) {for(int j = 0; j < n; j++) {if(matrix[i][j] - '0' == 0) {height[j] = 0;} else {height[j] ++;}}result = Math.max(result, maxHistArea(height));}return result;}public int maxHistArea(int[] hist) {if(hist == null || hist.length == 0) {return 0;}Stack<Integer> st = new Stack<>();int area = 0;int n = hist.length;for(int i = 0; i < n; i++) {while(!st.isEmpty() && hist[st.peek()] > hist[i]) {int cur = st.pop();area = Math.max(area, hist[cur] * (st.isEmpty() ?i:i-st.peek()-1));}st.push(i);}while(!st.isEmpty()) {int cur = st.pop();area = Math.max(area, hist[cur] * (st.isEmpty() ?n:n-st.peek()-1));}return area;}

leetcode 85. Maximal Rectangle(最大矩形)相关推荐

  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 85. Maximal Rectangle --python,java解法

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

  4. 85. Maximal Rectangle最大矩形

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

  5. 【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 ...

  6. [leetcode]@python 85. Maximal Rectangle

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

  7. 85. Maximal Rectangle 由1拼出的最大矩形

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

  8. 085 Maximal Rectangle 最大矩形

    给定一个填充了 0 和 1 的二进制矩阵,找到最大的只包含 1 的矩形并返回其面积. 例如,给出以下矩阵: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 返回 6 详 ...

  9. 85. Maximal Rectangle

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

  10. LeetCode 笔记系列 18 Maximal Rectangle [学以致用](最大矩形)

    leetcode之Largest Rectangle in Histogram 标签: leetcode面试题最大矩形堆栈单调队列 2016-07-30 13:47 1325人阅读 评论(0) 收藏  ...

最新文章

  1. 记录一次http请求失败的问题分析
  2. 迪普工业以太网交换机产品线
  3. 整数阶贝塞尔函数c语言,整数阶复宗量变形贝塞尔函数的计算.pdf
  4. 清华计算机考研笔记,[考研天地]清华计算机,电子,自动化全套考研资料
  5. java中如何播放wav_如何播放.wav文件与java
  6. 【Java】@transient代表着什么
  7. Java排序算法——猴子排序(Bogo Sort)
  8. eclipse中文语言包 离线安装
  9. 林子雨大数据技术原理总结
  10. 淘宝双十一最强星秀猫组队人气比拼活动规则和攻略
  11. 计蒜客 青出于蓝胜于蓝
  12. user interface(用户界面)
  13. Android 强制设置应用横屏或竖屏
  14. Linux批量追加文件名后缀
  15. 深入理解Java类加载器(2):线程上下文类加载器
  16. 新概念英语(第三册)复习(原文)——Lesson 21 - Lesson 30
  17. ATM机多账号的实现
  18. Linux命令行技巧——使用目录栈进行导航
  19. 任务7、统计一组学生成绩
  20. k系列图形工作站联想服务器,供应联想ThinkStationD20系列图形工作站

热门文章

  1. CSS backdrop-filter 滤镜 毛玻璃效果
  2. Fresher练打字
  3. 【JavaScript】(二)俄罗斯方块
  4. 基于Python+网络爬虫的兼职招聘就业信息数据可视化分析
  5. 世界第 3 的滴滴裁员,求职必知独角兽公司排行榜
  6. 双手指代脑区,读文献参考,大脑分区记不住怎么办
  7. 李永乐老师讲一个量子计算机,量子计算机到底是什么东西啊?我原来以为就是普通计算机的这个方向。...
  8. 56款让你过目不忘的html表单模板源代码
  9. MySQL原理与实践(三):由三种数据结构引入MySQL索引及其特性
  10. OpenGL编程(四)改变窗口大小时保持图形的原形