【动态规划】leetcode - Maximal Square
称号:
Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
分析:
利用动态规划求解。建立一个类node。node中成员变量left记录每个点的左边有几个1(包含该点本身)、up记录上边有几个1(包含该点本身)、maxsize记录该点相应的最大正方形的边长(该点在正方形右下角)。若一个点是‘0’,则其相应的node是(0,0,0).
1、用变量res记录最大正方形的边长。
2、先依次处理输入矩阵matrix左上角那个点、第一行和第一列,求出这些位置的node值。
3、再依次遍历matrix剩下的点,对每个点求出node值。并更新res。
4、返回res*res.
class node
{
public:
int left,up,maxsize;
node():left(0),up(0),maxsize(0){}
node(int a,int b,int c):left(a),up(b),maxsize(c){}
}; class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty() || matrix[0].empty())
return 0; int rows=matrix.size(),cols=matrix[0].size();
int res=0;
vector<vector<node>> dp(rows,vector<node>(cols)); if(matrix[0][0]=='1')
{
res=1;
dp[0][0]=node(1,1,1);
}
for(int j=1;j<cols;++j)
{
if(matrix[0][j]=='1')
{
res=1;
dp[0][j]=node(dp[0][j-1].left+1,1,1);
}
}
for(int i=1;i<rows;++i)
{
if(matrix[i][0]=='1')
{
res=1;
dp[i][0]=node(1,dp[i-1][0].up+1,1);
}
} for(int i=1;i<rows;++i)
{
for(int j=1;j<cols;++j)
{
if(matrix[i][j]=='1')
{
dp[i][j].left=dp[i][j-1].left+1;
dp[i][j].up=dp[i-1][j].up+1;
if(matrix[i-1][j-1]!='1')
dp[i][j].maxsize=1;
else
{
int tmp=min(dp[i-1][j-1].maxsize+1,dp[i][j].left);
tmp=min(tmp,dp[i][j].up);
dp[i][j].maxsize=tmp;
}
res=max(res,dp[i][j].maxsize);
}
}
} return res*res;
}
};
版权声明:本文博主原创文章,博客,未经同意不得转载。
【动态规划】leetcode - Maximal Square的更多相关文章
- 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 ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- LeetCode Maximal Square
原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LintCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
随机推荐
- Mac OS X Yosemite安装Hadoop 2.6记录
整个安装过程分为四部分: 一. 安装Homebrew 二. ssh localhost 三. 安装Hadoop已经进行配置文件设置 (伪分布式) 四. 执行栗子 一. 安装Homebrew 採用H ...
- 导致Asp.Net站点重启的10个原因
原文:导致Asp.Net站点重启的10个原因 Asp.Net站点有时候会莫名其妙的重启,什么原因导致的却不得而知,经过一番折腾后,我总结了导致Asp.Net站点重启的10个原因 1. 回收应用程序池会 ...
- Jafka来源分析——Processor
Jafka Acceptor接受client而建立后的连接请求,Acceptor会将Socket连接交给Processor进行处理.Processor通过下面的处理步骤进行client请求的处理: 1 ...
- FlowLayoutPanel 内的控件怎么调换顺序?
lowLayoutPanel1.Controls.SetChildIndex("flowLayoutPanel中的控件",顺序索引)
- 系列五AnkhSvn
原文:系列五AnkhSvn AnkhSvn介绍 AnkhSVN是一款在VS中管理Subversion的插件,您可以在VS中轻松的提交.更新.添加文件,而不用在命令行或资源管理器中提交.而且该插件属于开 ...
- RH133读书笔记(6) - Lab 6 Adding New Filesystems to the Filesystem Tree
Lab 6 Adding New Filesystems to the Filesystem Tree Goal: Develop skills and knowlege related to par ...
- 微服务API Gateway
翻译-微服务API Gateway 原文地址:http://microservices.io/patterns/apigateway.html,以下是使用google翻译对原文的翻译. 让我们想象一下 ...
- mysql经常使用命令总结
MySQL经常使用指令(备查) 最经常使用的显示命令: 1.显示数据库列表. show databases; 2.显示库中的数据表: use mysql; show tables; 3.显示数 ...
- 左右TS分析流
字节.在TS流里能够填入非常多类型的数据.如视频.音频.自己定义信息等.他的包的结构为,包头为4个字节,负载为184个字节(这184个字节不一定都是有效数据.有一些可能为填充数据). 工作形式: 由于 ...
- 学习笔记之TCP/IP协议的重要性
1. 随处可见的协议 在计算机网络与信息通信领域里,人们常常提及"协议"一词.互联网中常 用的具有代表性的协议有IP.TCP.HITP等. 而LAN(局域网)中经常使用的协 ...