leetcode -- Maximal Rectangle TODO O(N)
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)的更多相关文章
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- LeetCode: Maximal Rectangle 解题报告
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [leetcode]Maximal Rectangle @ Python
原题地址:https://oj.leetcode.com/problems/maximal-rectangle/ 题意:Given a 2D binary matrix filled with 0's ...
- [LeetCode] Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Maximal Rectangle(good)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
随机推荐
- Linux命令-文件搜索命令:grep
选项: -A 数字:表示查看满足条件行后的N条记录 -B 数字:表示查看满足条件行前的N条记录 -C 数字:表示查看满足条件行前后各N条记录 more /etc/inittab 分页查看inittab ...
- Win10 环境安装tesseract-ocr 4.00并配置环境变量
Tesseract-OCR的Training简明教程 https://blog.csdn.net/blueheart20/article/details/53207176 一.安装: 选择对应版本,h ...
- Mysql 字符串函数 详解
字符串函数是最常用的一种函数了,如果大家编写过程序的话,不妨回过头去看看自己使用过的函数,可能会惊讶地发现字符串处理的相关函数占已使用过的函数很大一部分.MySQL中字符串函数也是最丰富的一类函数,表 ...
- Mysql 的位运算符详解,mysql的优先级
位运算是将给定的操作数转化为二进制后,对各个操作数每一位都进行指定的逻辑运算,得到的二进制结果转换为十进制数后就是位运算的结果.MySQL 5.0 支持6 种位运算符,如表4-4 所示. 可以发现,位 ...
- Java运行Python脚本的几种方式
由于在项目需要执行Python,找寻相关资料,总结出以下几种方式: 直接执行Python脚本代码 引用 org.python包 PythonInterpreter interpreter = new ...
- shell脚本中执行mysql命令
1.mysql -hhostname -uuser -ppsword -e "mysql_cmd" 2. mysql -hhostname -uuser -ppsword < ...
- error while loading shared libraries *.so.*
转自 http://my.oschina.net/u/561492/blog/192341 ubuntu12.04-64位编译Android4.2时出现问题:error while loading s ...
- bazel-编译多目标
demo2 使用bazel编译多目标示例,一个bianry,一个library. demo2目录树 ── demo2 ├── app │ ├── BUILD │ ├── func.cpp ...
- 获取Oracle数据库中字段信息
select t.DATA_PRECISION,t.DATA_SCALE,t.DATA_LENGTH,t.DATA_TYPE,t.COLUMN_NAME, t.NULLABLE,t.DATA_DEFA ...
- WPF集合
Dependency Property 依赖属性 http://www.cnblogs.com/HelloMyWorld/archive/2013/02/21/2920149.html Attache ...