Maximal Rectangle [LeetCode]
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]的更多相关文章
- Maximal Rectangle [leetcode] 的三种思路
第一种方法是利用DP.时间复杂度是 O(m * m * n) dp(i,j):矩阵中同一行以(i,j)结尾的所有为1的最长子串长度 代码例如以下: int maximalRectangle(vecto ...
- Maximal Rectangle leetcode java
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- LeetCode: Maximal Rectangle 解题报告
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- 【leetcode】Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 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 ...
随机推荐
- 关于HTTP GET请求的url中文参数编码
场景:前端用JS构造了一个GET请求,携带了一个中文的参数,通过Spring MVC传到后台以后解析中文是乱码. 1. 发送请求,从浏览器中捕获到http的请求内容如下: Remote Address ...
- CUBRID学习笔记 47 show
cubrid的中sql查询语法show c#,net,cubrid,教程,学习,笔记欢迎转载 ,转载时请保留作者信息.本文版权归本人所有,如有任何问题,请与我联系wang2650@sohu.com . ...
- LVM逻辑分区
视频:http://v.youku.com/v_show/id_XNTk2NzExMTg0.html?f=27874439&o=1&spm=0.0.playList.5!3~5~A.t ...
- 小米盒子连接老式电脑显示器(VGA接口)
家里闲置一台老式显示器,只有VGA接口,无HDMI高清接口; 小米盒子上有三个输出接口: 一个HDMI高清接口:HDMI接口输出的有音频信号和视频信号,现在买的电视机一般都有HDMI高清接口: 一个A ...
- Yii 如何渲染另一控制器中的视图。
(Yii)使用renderPartial调用另外一个控制器的视图 我们可以使用renderPartial访问存储在不同控制器的视图文件夹中的部分视图文件. 在Yii1.1.3中,我们使用双斜线“//” ...
- Ext.net 异常统一管理,铥掉可恶的 Request Failure
Ext.net 异常统一管理,铥掉可恶的 Request Failure 看着这样的框框是不是很不爽 灭他.也不难.. .如果全部页面都有继承一个自定义的父类 ..那整个项目代码量就只有几行了.. 单 ...
- java乱码问题(转)
参考: http://blog.csdn.net/beijiguangyong/article/details/7414247 http://www.zhihu.com/question/202126 ...
- JavaScript基础知识之——Location 对象详解
属性 描述 location.hash 设置或取得 URL 中的锚 location.host 设置或取得 URL 中主机(包括端口号) location.hostname 设置或取得 URL 中的主 ...
- 阻塞与非阻塞的IO网络读写
看我之前的文章就知道,一般对于网络读的socket,都会加上O_NONBLOCK,非阻塞的选项. int setnonblocking(int fd) { int old_option = fcntl ...
- 转:C的|、||、&、&&、异或、~、!运算
转自:C的|.||.&.&&.异或.~.!运算 位运算 位运算的运算分量只能是整型或字符型数据,位运算把运算对象看作是由二进位组成的位串信息,按位完成指定的运算,得到位 ...