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. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  2. Python线程event

    python线程的事件用于主线程控制其他线程的执行,事件主要提供了三个方法wait.clear.set 事件处理的机制:全局定义了一个“Flag”,如果“Flag”值为 False,那么当程序执行 e ...

  3. 函数适配器bind2nd 、mem_fun_ref 源码分析、函数适配器应用举例

    一.适配器 三种类型的适配器: 容器适配器:用来扩展7种基本容器,利用基本容器扩展形成了栈.队列和优先级队列 迭代器适配器:(反向迭代器.插入迭代器.IO流迭代器) 函数适配器:函数适配器能够将仿函数 ...

  4. android VLayout 全面解析

    概述 前不久.阿里新开源了2个东西,Atlas和vlayout.今天我来介绍下vlayout的使用. 在介绍前,先抱怨两句,阿里放开源出来,感觉就是让我们这群人给他们找bug~~我曾遇到一个奇怪的问题 ...

  5. 一起talk C栗子吧(第一百三十一回:C语言实例--C程序内存布局三)

    各位看官们,大家好.上一回中咱们说的是C程序内存布局的样例,这一回咱们继续说该样例.闲话休提,言归正转.让我们一起talk C栗子吧. 看官们,关于C程序内存布局的样例,我们在前面的两个章回都介绍过了 ...

  6. python pivot() 函数

    以下为python pandas 库的dataframe pivot()函数的官方文档: Reshape data (produce a “pivot” table) based on column ...

  7. mysql innodb的重要组件

    innodb包涵如下几个组件 一.innodb_buffer_pool: 1 它主要用来缓存数据与索引(准确的讲由于innodb中的表是由聚集索引组织的,所以数据只不是过主键这个索引的叶子结点). 二 ...

  8. mysql误删root用户或者忘记root密码解决方法

    解决方法一: 到其他安装了Mysql的服务器(前提是要知道该服务器上Mysql的root用户密 码),打开[Mysql的安装目录/var/mysql],将其中的user.frm.user.MYD.us ...

  9. ajax处理select下拉表单

    $('#gameid').change(function() { var gameid = $(this).val(); if (this.value != '') { $.ajax({ url: ' ...

  10. CYQ聊天遇到的问题

    action.Data["yj_id"].Value  用action.Get<int>("yj_id"); 这种写法安全 如果是代码里怎么判断,a ...