Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

[解题思路]

1.brute force

枚举所有sub-matrix(O(N^2), N = m*n) ,检查每个子矩阵是不是都是1,如果是更新最大面积,检查子矩阵是否都是1需要

花费O(N). 故总的时间为O(N^3) N = m*n

可以过小数据,大数据直接TLE

 public int maximalRectangle(char[][] matrix) {
// Start typing your Java solution below
// DO NOT write main() function
int m = matrix.length;
if(m == 0){
return m;
}
int n = matrix[0].length;
if(n == 0){
return n;
} return generateMaxArea(matrix);
} private static int generateMaxArea(char[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int maxArea = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
int subMatrixArea = enumerateSubMatrix(matrix, i, j);
if (subMatrixArea > maxArea) {
maxArea = subMatrixArea;
}
}
}
return maxArea;
} public static int enumerateSubMatrix(char[][] matrix, int i, int j) {
int m = matrix.length;
int n = matrix[0].length;
int subMatrixArea = 0;
for (int p = 0; p <= (m - i); p++) {
for (int q = 0; q <= (n - j); q++) {
int area = getSubMatrixArea(matrix, p, q, p + i - 1, q + j - 1);
if (area > subMatrixArea) {
subMatrixArea = area;
}
}
}
return subMatrixArea;
} private static int getSubMatrixArea(char[][] matrix, int p, int q, int i,
int j) {
for (int m = p; m <= i; m++) {
for (int n = q; n <= j; n++) {
if (matrix[m][n] == '0') {
return 0;
}
}
} return (i - p + 1) * (j - q + 1);
}

2.DP

令dp[i][j]表示点(i,j)开始向右连续1的个数,花费O(M*N)的时间可以计算出来

接着从每个点开始,将该点作为矩形左上角点,从该点开始向下扫描直到最后一行或者dp[k][j] == 0

每次计算一个矩形的面积,与最大面积进行比较,如最大面积小于当前面积则进行更新,总的时间复杂度为O(M*N*M)

 public int maximalRectangle(char[][] matrix) {
// Start typing your Java solution below
// DO NOT write main() function
int m = matrix.length;
if(m == 0){
return m;
}
int n = matrix[0].length;
int[][] dp = new int[m][n]; for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(matrix[i][j] == '0'){
continue;
} else {
dp[i][j] = 1;
int k = j + 1;
while(k < n && (matrix[i][k] == '1')){
dp[i][j] += 1;
k++;
}
}
}
}
int maxArea = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(dp[i][j] == 0){
continue;
} else{
int area = 0, minDpCol = dp[i][j];
for(int k = i; k < m && dp[k][j] > 0; k++){
if(dp[k][j] < minDpCol){
minDpCol = dp[k][j];
}
area = (k - i + 1) * minDpCol;
if(area > maxArea){
maxArea = area;
}
}
}
}
}
return maxArea;
}

leetcode -- Maximal Rectangle TODO O(N)的更多相关文章

  1. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  2. LeetCode: Maximal Rectangle 解题报告

    Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...

  3. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  4. [leetcode]Maximal Rectangle @ Python

    原题地址:https://oj.leetcode.com/problems/maximal-rectangle/ 题意:Given a 2D binary matrix filled with 0's ...

  5. [LeetCode] Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  6. [LeetCode] Maximal Rectangle(good)

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  7. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  8. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  9. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

随机推荐

  1. Android+手势识别详解

      今天就来把以前的学习文章与经验简单总结中出来吧,在这里我就直接把代码贴下来了,希望能给初学者做最佳的学习参考,也希望有更多的开发人员来加入 ANDROID开发团队,参与更多的创新方式的开发,好了, ...

  2. Python多线程1:threading

    threading模块提供了高级别的线程接口,基于低级别的_thread模块实现. 模块基本方法 该模块定了的方法例如以下: threading.active_count()         返回当前 ...

  3. WIN10 无法访问2003 server共享

    With Windows 10 v1803 or Spring Creators update released I have decided to do a fresh installation o ...

  4. python 操作redis之——HyperLogLog (八)

    #coding:utf8 import redis # python 操作redis之——HyperLogLog r =redis.Redis(host=") # 1.Pfadd 命令将所有 ...

  5. C数组逆序

    一.标准交换模式 /**** *标准交换模式 *实现数组的逆序,原理就是数组的首尾元素进行交换 ***/ #define N 5; int main(){ int array[N] = {15,20, ...

  6. pow(x,y)函数的实现算法(递归函数)

    函数pow(x,y)实现运算x^y,即x的y次方,这里x和y都为整数. 算法的基本思想是,减少乘法次数,重复利用结算结果,例如: x^4,如果逐个相乘的话,需要四次乘法.如果我们这样分解(x^2)*( ...

  7. SimpleDateFormat线程不安全及解决的方法

    一. 为什么SimpleDateFormat不是线程安全的? Java源代码例如以下: /** * Date formats are not synchronized. * It is recomme ...

  8. 17. Subsets【medium】

    Given a set of distinct integers, return all possible subsets. Notice Elements in a subset must be i ...

  9. iOS_文件上传进度条的实现思路-AFNettworking

    iOS_文件上传进度条的实现思路-AFNettworking //要上传的文件名,在这里我使用当前日期做为文件的名称 NSString * fileName =[NSString stringWith ...

  10. js 静态方法 静态变量 实例方法 实例变量

    1.静态方法的定义 Js代码   var BaseClass = function() {}; // var BaseClass=new Function(); BaseClass.f1 = func ...