LeetCode Maximal Rectangle(dp)
来源:互联网 发布:淘宝id怎么查 编辑:程序博客网 时间:2024/06/06 18:17
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
题意:给出一个由字符0和1组成的二维字符数组,找到最大的矩阵,其所有字符为1
思路:动态规划,用left[n]表示所在行的第n个元素的左边界为1的下标,right[n]表示所在行的第n个元素的右边界的下标,height[n]表示所在行的第n个位置连续1的个数,即高度
状态转移方程为
left[n] = max{left[n], cur_left}
right[n] = min{right[n], cur_right}
height[n] = height[n] + 1 if matrix[row][col] = 1
代码如下:
class Solution { public int maximalRectangle(char[][] matrix) { int row = matrix.length; if (row == 0) return 0; int col = matrix[0].length; int[] left = new int[col]; int[] right = new int[col]; int[] height = new int[col]; Arrays.fill(right, col); int res = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (matrix[i][j] == '1') height[j]++; else height[j] = 0; } int cur_left = 0; for (int j = 0; j < col; j++) { if (matrix[i][j] == '1') left[j] = Math.max(left[j], cur_left); else { left[j] = 0; cur_left = j + 1; } } int cur_right = col; for (int j = col - 1; j >= 0; j--) { if (matrix[i][j] == '1') right[j] = Math.min(right[j], cur_right); else { right[j] = col; cur_right = j; } } for (int j = 0; j < col; j++) { res = Math.max(res, (right[j] - left[j]) * height[j]); } } return res; }}
0 0
- 【Leetcode】Maximal Rectangle (DP)
- [LeetCode] Maximal Rectangle(!!!DP)
- LeetCode Maximal Rectangle(dp)
- Leetcode 85 - Maximal Rectangle(dp)
- 【LeetCode】Maximal Rectangle && Maximal Square
- Leetcode:Maximal Square & Maximal Rectangle
- LeetCode: Maximal Rectangle
- LeetCode Maximal Rectangle
- LeetCode: Maximal Rectangle
- [LeetCode] Maximal Rectangle
- [leetcode] Maximal Rectangle
- [LeetCode]Maximal Rectangle
- [leetcode]Maximal Rectangle
- LeetCode-Maximal Rectangle
- [leetcode] Maximal Rectangle
- Leetcode Maximal Rectangle
- [LeetCode] Maximal Rectangle
- leetcode Maximal Rectangle
- 小流氓天空下载(skycn.com)
- leetcode287
- advanced-textview展示
- X字首类破解补丁速查
- W字首类破解补丁速查
- LeetCode Maximal Rectangle(dp)
- js鼠标跟随移动显示表格上某一行的信息(包括表格外的信息)
- 《数据结构与算法分析》动态规划--矩阵乘法最优顺序、最优二叉查找树详解
- Java 打印XML
- 查分约束POJ3169Layout解题报告
- P字首类破解补丁速查
- 在eclipse导入Java 的jar包的方法 JDBC【图文说明】
- Codeforces Beta Round #5——C. Longest Regular Bracket Sequence
- 二维数组求鞍点 ---- 2015/9/29