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.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

这题的DP思想部分借鉴了jianchao.li.fighter

思路如下:构建二维数组len,len[i][j]表示以(i,j)为右下角的最大方块的边长。

递推关系为 len[i][j] = min(min(len[i-1][j], len[i][j-1]), len[i-1][j-1]) + 1;

如下图示意:

以(i,j)为右下角的最大方块边长,取决于周围三个位置(i-1,j),(i,j-1),(i-1,j-1),恰好为三者最小边长扩展1位。

若三者最小边长为0,那么(i,j)自成边长为1的方块。

class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty() || matrix[].empty())
return ;
int m = matrix.size();
int n = matrix[].size();
int maxLen = ;
vector<vector<int> > len(m, vector<int>(n, ));
// first row
for(int i = ; i < n; i ++)
{
len[][i] = (int)(matrix[][i]-'');
if(len[][i] == )
maxLen = ;
}
// first col
for(int i = ; i < m; i ++)
{
len[i][] = (int)(matrix[i][]-'');
if(len[i][] == )
maxLen = ;
}
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(matrix[i][j] == '')
len[i][j] = ;
else
{
len[i][j] = min(min(len[i-][j], len[i][j-]), len[i-][j-]) + ;
maxLen = max(len[i][j], maxLen);
}
}
}
return maxLen * maxLen;
}
};

【LeetCode】221. Maximal Square的更多相关文章

  1. 【LeetCode】221. Maximal Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...

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

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

  3. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

  5. 【LeetCode】85. Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  6. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

  7. 【LeetCode】085. Maximal Rectangle

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's ...

  8. 【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221

    package y2019.Algorithm.dynamicprogramming.easy; /** * @ProjectName: cutter-point * @Package: y2019. ...

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

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

随机推荐

  1. 深度学习哪家强?吴恩达、Udacity和Fast.ai的课程我们替你分析好了

    http://www.jianshu.com/p/28f5473c66a3 翻译 | AI科技大本营(rgznai100) 参与 | reason_W 引言 过去2年,我一直积极专注于深度学习领域.我 ...

  2. 【转载】.NET/C#-uploadify视频文件or大文件上传

    引言 之前使用Uploadify做了一个上传图片并预览的功能,今天在项目中,要使用该插件上传大文件.之前弄过上传图片的demo,就使用该demo进行测试.可以查看我的这篇文章: [Asp.net]Up ...

  3. JAVA-错误Several ports (8080, 8009) required by Tomcat v7.0 Server at localhost are already in use.

    运行java程序的时候显示如下错误: 问题原因:导致这个错误的原因是因为已经启动了一个tomcat服务器 解决办法:到任务管理器中显示所有用户进程,将tomcat服务进程关闭,重新运行java程序就可 ...

  4. Linq-排序Order By

    适用场景:对查询出的语句进行排序,比如按时间排序等等. 说明:按指定表达式对集合排序:延迟,:按指定表达式对集合排序:延迟,默认是升序,加上descending表示降序,对应的扩展方法是OrderBy ...

  5. OpenGL ES 3.0之Fragment buffer objects(FBO)详解(二)

    我们可以使用帧缓冲对象来实现离屏渲染.帧缓冲对象支持下列操作 1.只使用OpenGL ES 函数创建帧缓冲区对象. 2.使用EGL context创建多个FBO. 3.创建离屏颜色.深度.模板渲染缓冲 ...

  6. VMware 安装Arch Linux记录

    首先说明一下我的环境. 1.VMware Workstation 10.0.1 build-1379776 2.archlinux-2014.02.01-dual.iso 首先建立虚拟机,其他的不提了 ...

  7. WIN10系统 如何自动调整系统时间

    在底部搜索框中输入设置   先关闭再打开自动设置时间的选项即可重新获取系统时间,前提要联网状态

  8. Jenkins 集成Unity3D Xcode

    如果Mac 上没有安装brew.先安装:ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)& ...

  9. ZH奶酪:PHP (爬虫)下载图片

    原文地址:http://www.phpfensi.com/php/20140107/1128.html 通过图片地地址把图片保存到本址,这里我们直接通过readfile读取然后通过fopen保存即可, ...

  10. fread/fwrite

    fread/fwrite第二个参数和第三个参数的区别以及与返回值的关系 size_t fwrite_unlocked(const void * __restrict ptr, size_t size, ...