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.

动态规划求解:

设dp[i][j]表示以matrix[i][j]结尾的最大正方形,则初始化:dp[i][j]=matrix[i][j],0<=i<=m,0<=j<=n(m,n为matrix的行数和列数)

状态转移方程:

dp[i][j] = MIN(dp[i-1][j-1],dp[i-1][j],dp[i][j-1])+1,if matrix[i-1][j-1] == '1' and matrix[i][j] == '1' and matrix[i-1][j] == '1' and matrix[i][j-1] == '1'

代码:

class Solution {
#define MIN(a,b,c) (a)<=(b)?((a)<=(c)?(a):(c)):((b)<=(c)?(b):(c))
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty())
{
return 0;
}
int m = matrix.size();
int n = matrix[0].size();
int dp[m+1][n+1];
int max = 0;
for(int i = 0;i < m;i ++)
{
for(int j = 0;j < n;j++)
{
if(matrix[i][j] == '1')
dp[i][j] = 1;
else
dp[i][j] = 0;
if(max < dp[i][j])
max = dp[i][j];
}
}
for(int i = 1;i < m;i ++)
{
for(int j = 1;j < n;j++)
{
if(matrix[i-1][j-1] == '1' && matrix[i][j] == '1' && matrix[i-1][j] == '1' && matrix[i][j-1] == '1')
{
int n = MIN(dp[i-1][j-1],dp[i-1][j],dp[i][j-1]);
dp[i][j] = n + 1;
}
else
{
}
if(max < dp[i][j])
max = dp[i][j];
}
}
return max * max;
}
};

leetcode之Maximal Square的更多相关文章

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

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

  2. [LeetCode] 221. 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. Java for LeetCode 221 Maximal Square

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

  4. (medium)LeetCode 221.Maximal Square

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

  5. [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming

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

  6. Leetcode 221. Maximal Square

    本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...

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

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

  8. 【动态规划】leetcode - Maximal Square

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

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

随机推荐

  1. 初次使用ets

    一.new(Name, Options) -> tid() | atom(),创建ets表. Options = [Option], 目测常用的属性, {keypos, Pos}:指定key的位 ...

  2. JavaSE(八)集合之Set

    今天这一篇把之前没有搞懂的TreeSet中的比较搞得非常的清楚,也懂得了它的底层实现.希望博友提意见! 一.Set接口 1.1.Set集合概述 Set集合:它类似于一个罐子,程序可以依次把多个对象 “ ...

  3. MathType给公式底部加箭头的教程

    箭头符号在数学中很常用的一个符号了,不管是在推导过程用以表示逻辑关系,还是表示向量,箭头符号都起着它就的作用.我们经常习惯给公式或者字母的上部加上箭头,那如何给公式的底部加上箭头呢?下面来介绍word ...

  4. git for c#, commit本地,pushserver

    //ok private static void push() { string wkDir = @"E:\DotNet2010\单位project\Git.Client\lib2Test\ ...

  5. 微软Azure、谷歌GAE、亚马逊AWS比較

       谷歌Google App Engine 亚马逊AWS 微软Microsoft Azure 提供服 务类型 PaaS, SaaS Iaas, PaaS IaaS, PaaS, SaaS 服务间 ...

  6. 超全面的JavaWeb笔记day02<CSS&JavaScript>

    1.CSS的简介 2.CSS概述和与HTML的结合方式(四种)(*******) 3.CSS的基本选择器(******) 4.CSS的扩展选择器(了解) 5.CSS的盒子模型(了解) 6.CSS的布局 ...

  7. MVC源码

    http://aspnetwebstack.codeplex.com/ MVC源码

  8. iOS 开发自定义一个提示框

    在开发的时候,会碰到很多需要提示的地方,提示的方法也有很多种,ios 8 以前的版本有alertview还是以后用的alertController,都是这种作用, 但是不够灵活,而且用的多了,用户体验 ...

  9. iPad UIPopoverController弹出窗口的位置和坐标

    本文转载至:http://blog.csdn.net/chang6520/article/details/7921181 TodoViewController *contentViewControll ...

  10. vs2008设置dll、lib库的输出路径

    vs2008中,有些项目上的功能是要生产库文件给其他项目调用的,以下是一些设置库文件(x.dll和x.lib)输出路径的方法. 设置x.dll 输出路径方法是在右键项目的"属性"- ...