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. 【转载】nedmalloc结构分析

    原文:nedmalloc结构分析 nedmalloc是一个跨平台的高性能多线程内存分配库,很多库都使用它,例如:OGRE.现在我们来看看nedmalloc的实现 (以WIN32部分为例)    位操作 ...

  2. Map Columns From Different Tables and Create Insert and Update Statements in Oracle Forms

    This is one of my most needed tool to create Insert and Update statements using select or alias from ...

  3. [SAP ABAP开发技术总结]动态修改选择屏幕

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. C# 文件流相关操作

    二进制转换成图片: MemoryStream ms = new MemoryStream(bytes); ms.Position = ; Image img = Image.FromStream(ms ...

  5. C# 获取打印机列表以及串口

    C# 获取打印机列表以及默认打印机.串口列表. /// <summary> /// 获取本地已安装的打印机 /// </summary> /// <returns> ...

  6. python内置函数的归集

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...

  7. Entity Framework 学习初级篇--基本操作:增加、更新、删除、事务(转)

    摘自:http://www.cnblogs.com/xray2005/archive/2009/05/17/1458568.html 本节,直接写通过代码来学习.这些基本操作都比较简单,与这些基本操作 ...

  8. ACDC

    acdc dcdc电源模块中大功率一般都是开关电源模式的,所以一般输入都是一个较宽的电源范围,体积也相对于变压器要小一些,效率高一些,但是纹波会偏大一些,如何选择就要看电路的需求来选择相应的方案

  9. 构件工具Maven----坐标、依赖、仓库、生命周期的简单学习

    这篇文章对Maven中几个比较重要的概念坐标.依赖.仓库.生命周期做一个简单的介绍. 1.关于Maven坐标 用来区别Maven世界中任何一个构件,Maven坐标的元素包括groupId.artifa ...

  10. epoll函数与参数总结学习 & errno的线程安全

    select/poll被监视的文件描述符数目非常大时要O(n)效率很低:epoll与旧的 select 和 poll 系统调用完成操作所需 O(n) 不同, epoll能在O(1)时间内完成操作,所以 ...