[LeedCode OJ]#85 Maximal Rectangle
联系信箱:libin493073668@sina.com】
class Solution
{
public:
int maximalRectangle(vector<vector<char> >& matrix)
{
int n = matrix.size();
if(n==0) return 0;
int m = matrix[0].size();
int i,j,c;
vector<vector<int> > dp,a;
dp.resize(n+1),a.resize(n+1);
for(i = 0; i<=n; i++)
{
dp[i].resize(m+1);
a[i].resize(m+1);
}
for(i = 0; i<n; i++)
{
for(j = 0; j<m; j++)
{
a[i+1][j+1] = matrix[i][j]-'0';
}
}
int sum = 0;
//计算1的个数
for(i = 1; i<=m; i++)
{
sum+=a[1][i];
dp[1][i] = sum;
}
for(i = 2; i<=n; i++)
{
sum = 0;
for(j = 1; j<=m; j++)
{
sum+=a[i][j];
dp[i][j]=dp[i-1][j]+sum;
}
}
//以每一个1为右下角,寻找最大全1子矩阵
int maxn = 0;
for(i = n; i>0; i--)
{
for(j = m; j>0&&maxn<i*j; j--)
{
if(a[i][j])
{
int r = 1,c = 1;
while(j-c>=0)
{
if(dp[i][j]-dp[i][j-c]-dp[i-r][j]+dp[i-r][j-c]==r*c)//是全1矩阵。继续增大列
{
maxn = max(maxn,r*c);
c++;
}
else
break;
}
while(i-r>=0&&c>0)
{
if(dp[i][j]-dp[i][j-c]-dp[i-r][j]+dp[i-r][j-c]==r*c)//是全1矩阵。继续增大行
{
maxn = max(maxn,r*c);
r++;
}
else//否则。降低一列再去反复推断
c--;
}
}
}
}
return maxn;
}
};
[LeedCode OJ]#85 Maximal Rectangle的更多相关文章
- LeetCode OJ 85. Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 85. Maximal Rectangle (Graph; Stack, DP)
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 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形
1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...
随机推荐
- MySQL基本命令和常用数据库对象
MySQL基本命令: 连接远程主机的MySQL服务(为了保证安全性,执行下面命令时,可以省略-p后面的密码,执行命令后系统会提示输入密码) mysql -p 密码 -u 用户名 -h 主机地址 --d ...
- Selenium WebDriver- 通过源码中的关键字找到我们要操作的句柄,用于多个窗口之间切换
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...
- 大数据学习——actor编程
1 概念 Scala中的Actor能够实现并行编程的强大功能,它是基于事件模型的并发机制,Scala是运用消息(message)的发送.接收来实现多线程的.使用Scala能够更容易地实现多线程应用的开 ...
- leetcode with python -> tree
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...
- HDU——1205吃糖果(鸽巢原理)
吃糖果 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submis ...
- bzoj 1196 公路修建问题
bzoj 1196: [HNOI2006]公路修建问题 Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情 ...
- 【POJ2104】K-th Number(主席树)
题意:有n个数组成的序列,要求维护数据结构支持在线的下列两种操作: 1:单点修改,将第x个数修改成y 2:区间查询,询问从第x个数到第y个之间第K大的数 n<=100000,a[i]<=1 ...
- 【CF659E】New Reform(图的联通,环)
分析转载自http://blog.csdn.net/yukizzz/article/details/51029628 题意: 给定n个点和m条双向边,将双向边改为单向边,问无法到达的顶点最少有多少个? ...
- Oracle Dual 表详解
1.DUAL表的用途Dual 是 Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中--查看当前连接用户SQL> select user from dua ...
- 【MFC】error RC2108: expected numerical dialog constant(转)
原文转自 http://blog.csdn.net/renyhui/article/details/23120469 [解决方案]在控件ID后面添加 "Static", SS_BI ...