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 ...
随机推荐
- 对 strcpy_s 若干测试
今天发现如果strcpy这函数,目标buffer太小,会有意想不到的崩溃.而且不容易调试.以后尽量要用strcpy_s了. strcpy_s是strcpy的更安全的版本 1.当目标字符串参数是一个字符 ...
- Examples For PLSQL Cursors - Explicit, Implicit And Ref Cursors
A cursor acts logically as a pointer into a result set. You can move the cursor through the result s ...
- Linux链接库四(多个库文件链接顺序问题)
最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于unde ...
- 二维数组实现checkbox的分组多选
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <script language="j ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- MySQL数据表生成ER图
遇到没有说明文档的数据库的时候,一张一张表去分析,需要花费很长时间和精力. 幸好有一些工具,可以帮助我们生成ER图,这样看起来就一目了然: 下面我将自己的一次实践记录于下,供参考: 1.下载并安装工具 ...
- iOS - TouchLock 手势解锁
1.手势解锁的创建 代码封装见 QExtension QLockView.h #import <UIKit/UIKit.h> @interface QLockView : UIView / ...
- U盘安装XP_sp3
1. 用的是老毛桃的 U盘制作工具(百度云OsSkill --> 全部文件 --> 软件安装包 --> 老毛桃 --> Install_LMT_v9_2__Win_x86.ex ...
- POJ 3260 多重背包+完全背包
前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...
- [css] 浏览器字体和css设置字体之间的关系
原文链接:http://www.zhangxinxu.com/wordpress/2010/06/%E5%8F%AF%E7%94%A8%E6%80%A7%E4%B9%8B%E6%B5%8F%E8%A7 ...