[LeetCode 85]Maximal Rectangle (华为2015机试)
来源:互联网 发布:淘宝售假白名单 编辑:程序博客网 时间:2024/06/06 00:51
题目链接:maximal-rectangle
import java.util.Arrays;/** * Given a 2D binary matrix filled with 0's and 1's, * find the largest rectangle containing all ones and return its area. * */public class MaximalRectangle {//65 / 65 test cases passed.//Status: Accepted//Runtime: 292 ms//Submitted: 0 minutes ago//时间复杂度O(n ^ 2), 空间复杂度 O(n) public int maximalRectangle(char[][] matrix) { if(matrix.length == 0) { return 0; } int m = matrix.length; int n = matrix[0].length; int[] H = new int[n]; int[] L = new int[n]; int[] R = new int[n]; Arrays.fill(H, 0); Arrays.fill(L, 0); Arrays.fill(R, n); int ret = 0; for(int i = 0; i < m; ++i) { int left = 0; int right = n; for (int j = 0; j < n; j++) {if (matrix[i][j] == '1') {++H[j];L[j] = Math.max(L[j], left);} else {left = j + 1;H[j] = 0;L[j] = 0;R[j] = n;} } for (int j = n - 1; j >= 0; j --) {if (matrix[i][j] == '1') {R[j] = Math.min(R[j], right);ret = Math.max(ret, H[j] * (R[j] - L[j]));} else {right = j;} } } return ret; }}
给定一个矩阵,里面只包含‘0’和‘1’,求出给最大的正方形的边长,该正方形里面全是‘1’
public int maximalRectangle1(char[][] matrix) { if(matrix.length == 0) { return 0; } int m = matrix.length; int n = matrix[0].length; int[] H = new int[n]; int[] L = new int[n]; int[] R = new int[n]; Arrays.fill(H, 0); Arrays.fill(L, 0); Arrays.fill(R, n); int maxLen = 0; for(int i = 0; i < m; ++i) { int left = 0; int right = n; for (int j = 0; j < n; j++) {if (matrix[i][j] == '1') {++H[j];L[j] = Math.max(L[j], left);} else {left = j + 1;H[j] = 0;L[j] = 0;R[j] = n;} } for (int j = n - 1; j >= 0; j --) {if (matrix[i][j] == '1') {R[j] = Math.min(R[j], right);maxLen = Math.max(maxLen, Math.min(H[j], R[j] - L[j]));} else {right = j;} } } return maxLen; }
0 0
- [LeetCode 85]Maximal Rectangle (华为2015机试)
- LeetCode(85) Maximal Rectangle
- leetcode || 85、Maximal Rectangle
- leetcode 85:Maximal Rectangle
- leetcode 85: Maximal Rectangle
- LeetCode(85) Maximal Rectangle
- Leetcode (85) Maximal Rectangle
- leetcode 85: Maximal Rectangle
- LeetCode 85 Maximal Rectangle
- [Leetcode] #85 Maximal Rectangle
- [Leetcode 85] Maximal Rectangle
- [leetcode-85]Maximal Rectangle(c++)
- LeetCode 85 (Maximal-Rectangle)java
- Leetcode 85 - Maximal Rectangle(dp)
- [leetcode 85] Maximal Rectangle 待续
- LeetCode[85]::Maximal Rectangle C++
- LeetCode No.85 Maximal Rectangle
- Leetcode 85 Maximal Rectangle 推荐!
- 详解 Android 的 Activity 组件 转载
- BZOJ1529【DFS】
- POJ 1715- Hexadecimal Numbers(组合数学)
- 前端那些事儿(3) --- WEB架构的历史变迁
- stdafx.h作用
- [LeetCode 85]Maximal Rectangle (华为2015机试)
- 算法
- 寒假杭电acm5
- 模式之--工厂方法(Factory Method)未完
- py 批量修改文件名
- Import Data from csv file to Azure SQL DATABASE
- 投资要点
- 推荐
- ASCII 、GB2312、GBK、GB18030、unicode、UTF-8字符集编码详解