85. Maximal Rectangle

Given a rows x cols binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.

Example 1:

Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 6
Explanation: The maximal rectangle is shown in the above picture.

Example 2:

Input: matrix = [["0"]]
Output: 0

Example 3:

Input: matrix = [["1"]]
Output: 1

Constraints:

动态规划 针对当前位置求解面积,计算最大宽和高

DP 解决方案从第一行开始逐行进行。让第 i 行和第 j 列的最大矩形面积由 [right(i,j) - left(i,j)]*height(i,j) 计算。

所有 3 个变量 left、right 和 height 都可以由来自前一行的信息以及来自当前行的信息确定。所以它可以看作是一个DP解决方案。转移方程为:

  • left(i,j) = max(left(i-1,j), cur_left), cur_left 可以从当前行确定

  • right(i,j) = min(right(i-1,j), cur_right), cur_right 可以从当前行确定

  • height(i,j) = height(i-1,j) + 1,如果 matrix[i][j]=='1';

  • height(i,j) = 0, 如果 matrix[i][j]=='0'

代码如下。可以将循环组合以提高速度,但我将它们分开是为了使算法更清晰。

class Solution {public int maximalRectangle(char[][] matrix) {int rows = matrix.length;int cols = matrix[0].length;int[] left = new int[cols];int[] right = new int[cols];int[] height = new int[cols];Arrays.fill(right, cols);int max = 0;for (int r = 0; r < rows; r++) {int curLeft = 0, curRight = cols;for (int c = 0; c < cols; c++) {height[c] = matrix[r][c] == '1' ? (height[c] + 1) : 0;}for (int c = 0 ;c < cols; c++) {if (matrix[r][c] == '1') {left[c] = Math.max(left[c], curLeft);} else {left[c] = 0;curLeft = c + 1;}}for (int c = cols - 1; c >= 0; c--) {if (matrix[r][c] == '1') {right[c] = Math.min(right[c], curRight);} else {right[c] = cols;curRight = c;}}for (int c = 0; c < cols; c++) {max = Math.max(max, (right[c] - left[c]) * height[c]);}}return max;}
}

如果觉得这个算法不好理解,可以试试这个例子:

0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 1 1 1 1 0

从第0行到第2行的向量“左”和“右”如下

第 0 行:dp[0][*]

h: 0 0 0 1 0 0 0
l: 0 0 0 3 0 0 0
r: 7 7 7 4 7 7 7

第 1 行:dp[1][*]

h: 0 0 1 2 1 0 0
l: 0 0 2 3 2 0 0
r: 7 7 5 4 5 7 7

第 2 行:dp[2][*]

h: 0 1 2 3 2 1 0
l: 0 1 2 3 2 1 0
r: 7 6 5 4 5 6 7

向量“left”正在计算左边界。以 (i,j)=(1,3) 为例。在当前行 1 上,左边界位于 j=2。但是,因为 matrix[1][3] 是 1,所以还需要考虑上一行的左边界,即 3。所以 (1,3) 处的真正左边界是 3。

每一行都计算最大max,比如这里以第三行举例,dp[2][*]
公式为 Math.max(max, (r - l) * h)

0: max(0, (7 - 0)*0) = 0
1: max(0, (6 - 1)*1) = 5
2: max(5, (5 - 2)*2) = max(5, 6) = 6
3: max(6, (4 - 3)*3) = max(6, 3) = 6
4: max(6, (5 - 2)*2) = max(6, 6) = 6
5: max(6, (6 - 1)*1) = max(6, 5) = 6
6: max(6, (7 - 0)*0) = max(6, 0) = 6

我希望这个额外的解释能让事情更清楚。

参考

https://leetcode.com/problems/maximal-rectangle/discuss/29054/Share-my-DP-solution

算法: 最大矩形面积85. Maximal Rectangle相关推荐

  1. 【DP】LeetCode 85. Maximal Rectangle

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

  2. 85. Maximal Rectangle

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

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

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

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

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

  5. 85. Maximal Rectangle最大矩形

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

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

  7. [leetcode]@python 85. Maximal Rectangle

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

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

  9. Algorithm:C++语言实现之链表相关算法(单链公共结点问题、一般LCA、括号匹配、最长括号匹配、逆波兰表达式Reverse Polish Notation、直方图矩形面积、收集雨水问题)

    Algorithm:C++语言实现之链表相关算法(单链公共结点问题.一般LCA.括号匹配.最长括号匹配.逆波兰表达式Reverse Polish Notation.直方图矩形面积.收集雨水问题) 目录 ...

  10. 蒙特卡洛算法求矩形内切圆面积

    已知:矩形长宽皆为1,则内切圆半径为0.5.求:该圆面积(利用蒙特卡洛算法) 编程思路如下: 1,利用random.unifom生成0到1之间的数字,分别赋值给x,y. 2,判断生成点是否落在矩形区域 ...

最新文章

  1. 阿里终面:为什么SSD不能当做内存用?
  2. rtrim php,php rtrim() 格式化中文问题
  3. 【CodeForces】947 D. Picking Strings
  4. Cocos2d-x——导入Cocostudio资源
  5. Ubuntu18.04 安装 nvidia2080Ti显卡驱动
  6. nagios监控3306端口
  7. Java实验输出希腊字母表
  8. 音乐与现代计算机技术,计算机音乐技术在音乐教育中的应用.doc
  9. 独家 | GAN大盘点,聊聊这些年的生成对抗网络 : LSGAN, WGAN, CGAN, infoGAN, EBGAN, BEGAN, VAE
  10. 轻量级日志 Loki 全攻略
  11. stanza和DBPedia的安装与使用
  12. 超时空智慧办公白皮书(2023)
  13. win10照片查看器_Win10 下好用的免费无广告看图软件 XnView
  14. noi题库c语言 1.5答案,NOIP2004提高组复赛试题答案c语言版
  15. CTeX:中英文混排无法正常换行+字体调整+行距调整
  16. matlab 游戏手柄,QtGamepad模块与游戏手柄交互小示例
  17. mysql中的四大运算符种类汇总20多项,用了三天三夜来整理的,还不赶快收藏
  18. unity怎么导入系统的树_Unity5.0_树_软件教程_资源库
  19. Redis(十)——HyperLogLog 基数统计和 Bitmap位图场景详解
  20. NYOJ 会场安排问题

热门文章

  1. 计算机组装检测与数据恢复,计算机检测维修与数据恢复.doc
  2. 3.mysql的主从原理是什么_mysql 的主从实现原理
  3. pycharm 默认pip安装位置_为什么电脑所有软件默认安装位置都是C盘?
  4. python模拟足球比赛_博客园仿真足球竞赛平台Python版SDK
  5. c语言中rm无法删除文件夹,rm:无法删除'-rf':没有这样的文件或目录
  6. 在计算机网络GAN代表什么,图解 生成对抗网络GAN 原理 超详解
  7. Windows下 Nginx创建文件服务器
  8. Rails开发细节《六》ActiveRecord Validationa and Callbacks验证和回调
  9. 使用php glob函数查找文件,遍历文件目录(转)
  10. Serv-u6.0提权新玩法