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-软件包管理-源码包安装
rpm -q gcc 查看c语言编译器是否已经安装 在浏览器输入:http://mirror.bit.edu.cn/apache/httpd/ 下载2.2.29这个包 cd ~ 回到root目录 ls ...
- FFmpeg进行屏幕录像和录音
文章转自:http://www.cucer.cn/2016/03/10/ffmpeg-screen-capture.html 有些时候我们需要对屏幕进行录制,比如制作视频教程,录制直播等.然而这方面的 ...
- 黑马程序猿——25,打印流,合并流,对象序列化,管道流,RandomAccessFile
------<ahref="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...
- 查看linux系统某宏的定义(另类)
有一次我想查看time_t类型的定义,一开始想直接在/usr/include/time.h中查找,结果里面没有,而是转到另外一个:__time_t(2个下划线), 结果又转到:__STD_TYPE _ ...
- 通过CLR API实现C++调用C#代码交互
对于某些跨平台程序,这也就意味着只能在windows下使用了 不过最近.Net开源或许以后可以跨平台 之前花了一些时间研究COM方式调用,太繁琐不推荐. COM方式调用总结 后来尝试使用CLR C++ ...
- mysql 主主复制(双主复制)binlog-do-db
[root@DB ~]# grep "binlog-do-db" /etc/my.cnf binlog-do-db = test [root@DB-S ~]# grep " ...
- swift 函数.和匿名函数
函数 注意: 没有定义返回类型的函数会返回特殊的值,叫 Void.它其实是一个空的元组(tuple),没有任何元素,可以写成(). 使用元组作为返回参数,返回多个参数 func count(strin ...
- 成为 Team Leader 后我最关心的那些事
成为 Team Leader 后我最关心的那些事 推荐序 老有人问我 iOS 开发如何提高,今天收到一个来自网易的朋友投稿,分享他在成为 iOS 项目负责人之后面临的问题.文章中分享的如何招人,如 ...
- codeblocks 配置OpenGL
一.选择编译器环境 这里选择codeblocks,带MinGW的版本. 二.下载glut工具包 网址:http://pan.baidu.com/s/1eQriTQM 三.配置glut 解压缩下载的gl ...
- 删除outlook配置信息
1.输入“Win+R”组合键,在弹出的窗口中输入:control,打开控制面板 2.找到“邮件”选项,并单击 3.在弹出的窗口中,单击“显示配置文件”选项,删除配置文件夹,OK.