一开始参考http://www.geeksforgeeks.org/maximum-size-sub-matrix-with-all-1s-in-a-binary-matrix/,但发现它的条件是square submatrix,所以本题不符合。
后来参考http://yucoding.blogspot.com/2013/01/incomplete-leetcode-question-47-maximal.html写的很详细了,转抄于此:
Analysis:
This is not an easy problem and the idea is not straightforward.Since the question requires the area of all ones region, first we can define the region (rectangle) in the matrix. Any region in the
matrix has several basic properties: 1 corner point, width, and length.Also which corner point it is decide the exact place of the region, here we consider the bottom-right corner point, because we usually
scan the matrix from (0,0) to right and down direction. For this problem, we also need to consider the "all ones" requirement, where all the regions are filled with 1s.We can then have this data structure:One 2D array(vector) ones[i][j], where ones[i][j] stores the number of contiguous 1s ended at matrix[i][j], along ith row. e.g. if
matrix[i] = "1011101", then ones[i]=1,0,1,2,3,0,1. This array stores the length of the rectangle, for every possible bottom-right corner point.Having this two values, next is to find the height of the rectangle, as well as keep the "all ones" requirement.Start with a corner point [i][j], since it is the bottom right corner, the rectangle search direction is left and up.For the left we already have the number of contiguous 1s ones[i][j]. How to get the height? We need to scan all the values above ones[i][j],
it is from i-1 to 0. If meets 0, then stop, else compute the possible rectangle area, and store the max. To compute the area, the minimum length of rectangle should be compared and stores every time.
package Level5;
/**
* Maximal Rectangle
*
* 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 S85 {
public static void main(String[] args) {
char[][] matrix = {{'0','1'},{'0','1'}};
System.out.println(maximalRectangle(matrix));
}
public static int maximalRectangle(char[][] matrix) {
int rows = matrix.length;
if(rows == 0){
return 0;
}
int cols = matrix[0].length;
// 先计算全1矩阵的宽度
int[][] hOnes = new int[rows][cols]; // horizontal ones
int max = 0; // 最大面积
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(matrix[i][j] == '1'){
if(j == 0){ // 特殊处理左起第一个
hOnes[i][j] = 1;
}else{
hOnes[i][j] = hOnes[i][j-1] + 1;
}
}
}
}
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(hOnes[i][j] != 0){
int minI = i; // 计算高度,minI不断向上走
int minRowWidth = hOnes[i][j]; // 随着向上走,宽度也要随之调整
while(minI >= 0){
minRowWidth = Math.min(minRowWidth, hOnes[minI][j]);
int area = minRowWidth * (i-minI+1);
max = Math.max(max, area);
minI--;
}
}
}
}
return max;
}
}
分享到:
相关推荐
191 |[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C++](./C++/number-of-1-bits.cpp) [Python](./Python/number-of-1-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 201 | [Bitwise AND of ...
1. Introduction 2. Array i. Remove Element ii. Remove Duplicates from Sorted Array iii. Plus One iv. Pascal's Triangle v. Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. ...
求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈判断括号匹配 Regular Expression Matching 递归匹配 wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限...
leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 Java,部分有解释 Java Associated Documents and Resources Peter norvig神牛Python代码写的很飘逸,果然是有LISP...
力扣最大面积最大矩形 给定一个由 0 和 1 填充的二维二进制矩阵,找到仅包含 1 的最大矩形并返回其面积。 Example: Input: [ ["1","0","1","0","0"], ["1","0","1","1","1"], ["1","1","1","1","1"], ["1","0","0",...
leetcode 316 LeetCode Summary Exclusive Time of Functions: 栈 Friend Circles:DFS Print Binary Tree:二叉树 Maximal Square:DP Maximal Rectangle:单调栈(Histogram变形) Largest Rectangle in Histogram:...
lru缓存leetcode leetcode 大批 41. First Missing Positive 广度优先搜索 773. Sliding Puzzle 864. Shortest Path to Get All Keys 深度优先搜索 996. Number of Squareful Arrays 拓扑排序 269. Alien Dictionary...
Rectangle 栈 局部递增 或者 动态规划 Binary Tree Inorder Traversal 栈 递归 Single Number 异或 Copy List with Random Pointer 单链表 map Max Points on a Line 斜率 map, int> Fraction to Recurring Decimal ...
1.解压文件。运行Matlab,在Matlab中打开.../minepy/matlab/(当前文件夹为matlab); 2.在command window(命令行窗口)中输入:mex mine_mex.c ../libmine/mine.c; 3.测试代码: x = linspace(0, 1, 1001); y =...
数据挖掘相关内容Redundancy-Aware Maximal Cliques,关于最大派系
矩阵的每一行用 1 表示存在边,用 0 表示不存在边。行和列号。 边的表示连接节点。 给定这个矩阵,这个函数找到所有最大的完整子图(所有节点中的一组节点,形成一个完整的子图,即每个节点相互连接),也称为集团...
leetcode 和 oj 编程问题 在线裁判网站问题解决方案 如果您遇到这个问题并对如何更好地完成某些解决方案有建议或意见,欢迎您。 palin.ml : ...MaximalRectangle : oj.leetcode 问题的java解决方案
The set of maximal frequent subgraphs is much smaller to that of the set of frequent subgraphs, thus providing ample scope for pruning. MARGIN is a maximal subgraph mining algorithm that moves among ...
关于论文MaPle A Fast Algorithm for Maximal Pattern-based Clustering∗的翻译
关于最大流网络算法的一些介绍,是外国文献,最大网络流的应用
这个文档的质量相当高,不过是纯英文的,但是,我保证你看过之后,肯定会认为这个文档写得实在太好了。
2017 A Maximal Clique Based Multiobjective Evolutionary algorithm for overlapping community detection 论文PPT讲解
本资源是用C语言所写的,数据结构中图的创建及其相关的深度,广度遍历
Maximal Beasts
非齐次分数次 Schr,张军勇,郑继强,本文考虑如下极大不等式$$ ig|sup_{...1}|e^{itsqrt{1+(-Delta)^ lpha}}f(x)| ig|_{L^2(B(0,1))}leqC|f|_{H^s(mathbb R^d)},quad orall~fin H^s(mathbb R^d),quad lphageq1.$$ �