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. 转: Xshell鼠标选中,终端立即中断(CTRL-C)的问题

    转自: https://nkcoder.github.io/2014/05/05/xshell-select-interrupt-dict/ Xshell选中文字复制时中断 在Xshell中设置了“自 ...

  2. selenium之 chromedriver与chrome版本映射表(更新至v2.38)

    https://blog.csdn.net/huilan_same/article/details/51896672 看到网上基本没有最新的chromedriver与chrome的对应关系表,便兴起整 ...

  3. 【nodejs】理想论坛帖子下载爬虫1.08

    //====================================================== // 理想论坛帖子下载爬虫1.09 // 使用断点续传模式,因为网络传输会因各种原因中 ...

  4. 刷完OpenWrt在浏览器无法访问的解决办法

    其实问题很明显. 是因为刷了trunk版固件. 并没有集成luci. 那接下来就是装luci.但是装luci需要联网(不过其实不联网其实也是可以安装的.) 我说的联网是让路有联网.而不是网线接路由,路 ...

  5. 使用Snap.svg类库实现的抖动式的幻灯播放效果

    在线演示 本地下载 这个幻灯中,使用了SVG来生成具有动画弧度的幻灯背景效果,如果你在项目中能够支持现代浏览器的话,尝试一下这个效果吧,很赞! 想了解基础使用,观看这个轻视频吧:Snap.svg处理和 ...

  6. 去除Win10快捷图标小箭头

    有点强迫症,一看到操作系统上的快捷图标小箭头就想把它去除掉. 去除小箭头 reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Cur ...

  7. android中去掉ListView控件中的分割线

    通过设置android:divider="@null" ,可以去掉ListView控件中的分割线 也可以自定义分割线的颜色,比如: <ListView android:id= ...

  8. 使用Filter过滤非法内容

    1.首先,需要编写一个响应的封装器ResponseReplaceWrapper,用它来缓存response中的内容,代码如下: ResponseReplaceWrapper.java package ...

  9. file: /SourceCache/IOKitUser_Sim/IOKitUser-920.1.11/hid.subproj/IOHIDEventQueue.c, line: 512

    //修改main.m 文件. typedef int (*PYStdWriter)(void *, const char *, int); static PYStdWriter _oldStdWrit ...

  10. 【树莓派】树莓派上面安装配置teamviewer

    访问树莓派桌面,的另一种方式,就是使用Teamviewer. 参考这篇文章做了实验:http://www.linuxdiyf.com/linux/16887.html,对其中部分进行了件要整理和总结. ...