【动态规划】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 ...
随机推荐
- Ribbon 和 Eureka 积分
Ribbon 这是 Netflix 云服务的中间层宣布开放源代码项目,它的主要功能是提供客户机端软件的负载均衡算法,将 Netflix 中间层服务一起. Eureka 是 RESTfu ...
- 使用 svn+maven+jenkins(hudson)+Publish Over SSH plugins 构建持续集成及自动远程发布体系(转)
1.安装jenkins 2.浏览器访问jenkins主页 http://192.168.0.1:8080/,点击“系统管理” 3.在插件管理中,安装Publish Over SSH插件 4.在系统设置 ...
- 在java代码中获取JVM参数(转)
近日关注性能调优,关注JMX,发现java.lang.management.*之强大.同时查阅了资料,整合一版关于JVM参数获取的note,仅供参考: MemoryMXBean memorymbean ...
- 思维导图之C++语言程序设计总结
花了大约一周的时间,将c++的课本过了一遍,米老师说第一遍不求甚解,仅仅管去看就能够了,我很成功地运行了老师这种方法,嘿嘿.那么c++是什么呢?百度上这样说,它是一种使用很广泛的计算机编程语言.C++ ...
- 腾讯2014在广州站实习生offer经验(TEG-开发背景)
研究在过去的一年是linux 什么系统编程和网络编程.比较熟悉的语言c/c++,python只写一些测试client.是后台开发类,比方前面笔面的网易CC(面完hr后挂).大概3月15号就在腾讯 jo ...
- UVA 11464 Even Parity(递归枚举)
11464 - Even Parity Time limit: 3.000 seconds We have a grid of size N x N. Each cell of the grid in ...
- Ajax基础知识(一)
随便在百度谷歌上输入Ajax都会出现一大堆的搜索结果,这已经不再是什么新奇的技术了.但若从一开始就学习了ASP.Net,使用功能齐全的Visual Studio集成开发工具,或许未必能对访问一个动态网 ...
- [SignalR]异常信息捕获以及处理
原文:[SignalR]异常信息捕获以及处理 异常处理,一般采用try..catch方式处理,而signalR里面有HubPipelineModule类可以捕获到Hub内发生的异常信息. 从上图中,可 ...
- Shark集群搭建配置
一.Shark简单介绍 Shark是基于Spark与Hive之上的一种SQL查询引擎,官网的架构图及性能測试图例如以下:(Ps:本人也做了一个性能測试见Shark性能測试报告) 我们涉及到了2个依赖组 ...
- Codeforces Round #254 (Div. 2):A. DZY Loves Chessboard
A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...