Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 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.

class Solution {

public:

    inline int min(int x, int y) {

        return x<y? x:y;

    }

    inline int min(int x, int y, int z) {

        return min(x, min(y, z));

    }

    int maximalSquare(vector<vector<char>>& matrix) {

        int row = matrix.size();

        if (row <=) return ;

        int col = matrix[].size();

        int maxSize = ;

        vector<vector<int>> dp(row, vector<int>(col));

        for (int i=; i<matrix.size(); i++) {

            for (int j=; j<matrix[i].size(); j++){

                //convert the `char` to `int`

                dp[i][j] = matrix[i][j] -'';

                //for the first row and first column, or matrix[i][j], dp[i][j] is ZERO

                //so, it's done during the previous conversion

                // i>0 && j>0 && matrix[i][j]=='1'

                if (i!= && j!= & dp[i][j]!=){

                    dp[i][j] = min(dp[i-][j], dp[i][j-], dp[i-][j-]) + ;

                }

                //tracking the maxSize

                if (dp[i][j] > maxSize ){

                    maxSize = dp[i][j];

                }

            }

        }

        return maxSize*maxSize;

    }

};

*   1) P[0][j] = matrix[0][j] (topmost row);

*   2) P[i][0] = matrix[i][0] (leftmost column);

*   3) For i > 0 and j > 0:

*       3.1) if matrix[i][j] = 0, P[i][j] = 0;

*       3.2) if matrix[i][j] = 1, P[i][j] = min(P[i-1][j], P[i][j-1], P[i-1][j-1]) + 1.

221. Maximal Square -- 矩阵中1组成的最大正方形的更多相关文章

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

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

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

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

  3. LeetCode题解:(221) Maximal Square

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

  4. 【LeetCode】221. Maximal Square

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

  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 最大正方形

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

  7. 221. Maximal Square

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

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

  9. 221 Maximal Square 最大正方形

    在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积.例如,给出如下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 4. 详见:https://l ...

随机推荐

  1. shutdown命令

    其实不需要使用软件,就可以实现自动关机或重启等功能的:Windows XP的关机是由Shutdown.exe程序来控制的,位于Windows\System32文件夹中.如 果想让Windows 200 ...

  2. 在Spring中使用脚本

    Spring支持3中不同的脚本语言(看来支持地还挺多的嘛):JRuby.Groovy和BeanShell. 这三个都是java社区的脚本语言(反正到目前为止我一个都没用过,可见我有多挫). JRuby ...

  3. tesseract-ocr了解

    安装文件下载地址 http://tesseract-ocr.googlecode.com/files/tesseract-ocr-setup-3.01-1.exe README For the lat ...

  4. Mybatis+SpringMVC+Spring整合

    1,先添加spring支持: applicationContext.xml  配在WEBINF下,四个命名空间:aop,context,tx,p 配Listener:ContextLoaderList ...

  5. bzoj 1054: [HAOI2008]移动玩具 bfs

    1054: [HAOI2008]移动玩具 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 在 ...

  6. elastic

    学习链接 http://rfyiamcool.blog.51cto.com/1030776/1420811?utm_source=tuicool&utm_medium=referral

  7. poj2780Linearity(多点共线)

    链接 判断最多多少点在一条直线上, 可以枚举每一个点为坐标系的原点,其它点变成相应的位置,然后求得过原点及其点的斜率,排序找一下最多相同的. #include <iostream> #in ...

  8. ASP.NET MVC HtmlHelper用法集锦

    ASP.NET MVC HtmlHelper用法集锦 在写一个编辑数据的页面时,我们通常会写如下代码 1:<inputtype="text"value='<%=View ...

  9. 查看package编译时的错误信息及重新编译

    开发时,一般都是使用PL/SQL工具进行开发,查看编译错误及重新编译都很简单,但是一般的生产环境是不允许连接外界工具的,只能在命令行中进行重新编译及查看,今天我就遇到了这个问题,现在总结如下: 1.获 ...

  10. keepalived的安装和使用

    IP配置 管理IP地址 角色 备注 网卡 192.168.1.114 主调度器(Director) 对外提供VIP服务的地址为192.168.1.88 eth1 192.168.1.205 备用调度器 ...