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

Basic idea: To increas one dimension by one, then calculate the maximum of other dimension every time.

 class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(matrix.size() == || matrix[].size() == )
return ;
int max_area = ;
int row = matrix.size();
int column = matrix[].size();
for(int i = ; i < row; i ++) {
for( int j = ; j < column; j ++) {
if (matrix[i][j] == '')
continue; int tmp_area = ;
int max_row_idx = row - ;
int max_column_idx = j;
while(max_column_idx < column){
int l;
for(l = i + ; l <= max_row_idx; l ++){
if (matrix[l][max_column_idx] == '')
break;
}
max_row_idx = l - ;
tmp_area = (max_row_idx - i + ) * (max_column_idx - j + );
if(tmp_area > max_area)
max_area = tmp_area; //increase column
if ( matrix[i][max_column_idx + ] == ''){
max_column_idx ++;
}else{
break;
}
} }
}
return max_area;
}
};

Maximal Rectangle [LeetCode]的更多相关文章

  1. Maximal Rectangle [leetcode] 的三种思路

    第一种方法是利用DP.时间复杂度是 O(m * m * n) dp(i,j):矩阵中同一行以(i,j)结尾的所有为1的最长子串长度 代码例如以下: int maximalRectangle(vecto ...

  2. Maximal Rectangle leetcode java

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

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

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

  4. leetcode Maximal Rectangle 单调栈

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

  5. leetcode面试准备: Maximal Rectangle

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

  6. LeetCode: Maximal Rectangle 解题报告

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

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

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

  8. 【leetcode】Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  9. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

随机推荐

  1. CentOS 7一些常用配置

    一.更改启动模式 背景:个人开发环境,安装了GNOME桌面,默认启动是图形化模式. 修改为命令行模式. systemctl set-default multi-user.target 二.命令行模式下 ...

  2. Phonegap项目中禁用WebViewBounce

    UIWebView是iOS SDK中一个最常用的控件,在PhoneGap中,默认也是使用UIWebView作为默认视图显示我们的HTML应用的.   在使用PhoneGap的项目中,默认WebView ...

  3. 巧遇"drwxr-xr-x."权限

    drwxr-xr-x. 是SELinux的问题 REDHAT6之后安全提高所以设置的 关闭SELINUX就好了 因为新版本ls把多acl和selinux属性加进去了,与系统无关,新版本的ls代码使用1 ...

  4. [转]Linux下的暴力密码破解工具Hydra详解

    摘自:http://linzhibin824.blog.163.com/blog/static/735577102013144223127/ 这款暴力密码破解工具相当强大,支持几乎所有协议的在线密码破 ...

  5. Java开发中经典的小实例-(if(参数){}else{})

    import java.util.Scanner; public class Calculate {    public static void main(String[] args) {       ...

  6. 图解SQL 2008数据库复制

    为了达到数据及时备份,一般采用完整备份+差异备份即可,或者再加上日志备份,本文介绍使用数据库复制技术同步数据: PS:文章以图片为主,图片更能直观的看出操作步骤和配置方法! 1.首先创建一个测试的数据 ...

  7. iOS - Swift NSEnumerator 迭代器

    前言 public class NSEnumerator : NSObject, NSFastEnumeration 1.迭代器 let arr:NSArray = ["bei", ...

  8. iOS - UISegmentedControl

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UISegmentedControl : UIControl <NSCoding> @available ...

  9. Codeforces708C Centroids 【树形dp】

    题目链接 题意:给定一棵n个结点的树,问:对于每个结点,能否通过删除一条边并添加一条边使得仍是树,并且删除该结点后得到的各个连通分量结点数 <= n/2? 题解:树形dp,两遍dfs,第一遍df ...

  10. 我想有个梦想(I want have a dream)

    成东青说过:梦想是什么,梦想就是你坚持就觉得是幸福东西. 多好啊,有梦想,想想就觉得幸福的.也许你会觉得有点做作,但我真觉得是这样.没有梦想就像一个没有了灵魂的躯壳,整天浑浑噩噩,整天麻木的上班下班, ...