称号:

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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. LeetCode Maximal Square

    原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...

  4. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  5. 【刷题-LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  6. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  7. 【LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  8. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  9. [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 ...

随机推荐

  1. ios 多线程开发(一)简介

    简介 线程是在一个程序中并发的执行代码的方法之一.虽然有一些新的技术(operations, GCD)提供了更先进高效的并发实现,OS X和iOS同时也提供了创建和维护线程的接口. 这里将要介绍线程相 ...

  2. SVN命令汇总

    1.将文件checkout到本地文件夹  svn checkout path(path是server上的文件夹)  比如:svn checkout svn://192.168.1.1/pro/doma ...

  3. JS基础——函数的创建和使用

    在JS中函数在使用时实质上和我们平时学习的编程语言中的函数类似,它相同也具有函数名,參数,返回值,函数体等这些寻常函数所具有的内容.可是作为一种脚本语言,它确实也有自己不一样的地方. 一.创建 < ...

  4. poj2524

    说来惭愧啊..现在才会并查集.我竟然给我妈妈讲明白并查集怎么回事了- - #define _CRT_SECURE_NO_WARNINGS #include <iostream> using ...

  5. Android手机定位技术的发展

    基于以下三种方式的移动位置:1. 网络位置 :2. 基站定位. 3. GPS定位 1 网络位置 前提是连接到网络:Wifi.3G.2G 到达IP址  比如:彩虹版QQ,珊瑚虫版QQ,就有一个功能显示对 ...

  6. Visual Studio Team Services使用教程--邀请团队成员

  7. HDU 3081 Marriage Match II(二分法+最大流量)

    HDU 3081 Marriage Match II pid=3081" target="_blank" style="">题目链接 题意:n个 ...

  8. paip.自适应网页设计 同 响应 与设计的原理的差and实践总结

    paip.自适应网页设计 同 响应 与设计的原理的差and实践总结 响应式Web设计(Responsive Web design)的理念是: 1 #-----------自适应布局VS响应式布局 2 ...

  9. ZOJ 3822 可能性DP

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822 本场比赛之前,我记得.见WALK概率路DP称号.那么它应该是可以考虑 ...

  10. 【 Android官方文件读书笔记】连接网络

    一间连接应用网络的主要功能.Android系统对网络连接进行了封装,使得开发人员可以更快的给应用添加网络功能.大多数网络连接的Android应用使用HTTP发送和接受数据.Android包含两个HTT ...