【动态规划】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 ...
随机推荐
- fcitx
安装完成后首先面对的问题是全英文无法显示汉字和输入汉字的问题.需要找到光盘上的2个中文支持字体包安装上即可.文件名如下,注意不同版本文件明后面数字部分可能会不同,其代表版本号.下面是Red Hat E ...
- 2014阿里巴巴web前实习生项目分析(1)
以下简化CSS代码: div.container{ width:500px; background-image:url(/img/sprite.png); background-repeat:no-r ...
- 改变,从跨出第一步開始——记海大ITAEM团队首次IT讲座掠影
之前以前写了"行动带来力量,周三(5月7日)晚IT讲座通知",昨晚已经跨出了第一步.让我们还是看举办者骆宏的QQ空间的文字吧.尽管未能到现场助兴,但看着海大学子在不断挑战自己,进而 ...
- HDU 1054 Strategic Game(树形DP)
Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he ...
- Redis数据结构
Redis数据结构 Redis数据结构详解(一) 前言 Redis和Memcached最大的区别,Redis 除啦支持数据持久化之外,还支持更多的数据类型而不仅仅是简单key-value结构的数据 ...
- 嵌入在网页上Flash媒体播放器(1)
做的项目,在不久的将来相关的Flash玩家使用,需要播放视频的网页上,不同的视频资源,需要不同的球员.基于使用稳定性.的嵌入式和嵌入式复杂性能的优点概括起来有两种方式(不同的玩家),视频资源也略有不同 ...
- cocos2d-x lua 学习笔记(1) -- 环境结构
Cocos2d-x 3.0超过环境的版本号来建立和前Cocos2d-x 2.0 差异较大的版本,从同时Cocos2d-x 3.0项目打包成apkAndroid的应用程序文件,步骤,构建环境有些乏味安德 ...
- DM8168 新三板系统启动
DM8168从补丁到系统的新董事会开始折腾了20天,最终完成,高校是累的东西,导师只焊接机10一个BGA,其他人则手. 前段时间启动操作系统时,到了Starting Matrix GUI applic ...
- DBA工具——DMV——通过sys.dm_exec_procedure_stats查看存储过程执行信息
原文:DBA工具--DMV--通过sys.dm_exec_procedure_stats查看存储过程执行信息 对于DBA来说,经常要手机存储过程的某些信息: 执行了多少次 执行的执行计划如何 执行的平 ...
- Team Foundation Server 2015使用教程--新增权限为读取器的团队