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. Django忘记管理员账号和密码的解决办法

    看着Django的教程学习搭建网站,结果忘记第一次创建的账号和密码了.结果搭建成功以后,一直无法登陆到管理页面,进行不下去了. 如图所示: 在网上找了很多的方法都不行,最后使用新建一个superuse ...

  2. ARM指令分类学习

    指令分类: 1.算数和逻辑指令 2.比较指令 3.跳转指令 4.移位指令 5.程序状态字访问指令 6.存储器访问指令 +++++++++++++++++++++++++++++++++++++++++ ...

  3. mybatis的基本配置:实体类、配置文件、映射文件、工具类 、mapper接口

    搭建项目 一:lib(关于框架的jar包和数据库驱动的jar包) 1,第一步:先把mybatis的核心类库放进lib里

  4. jQuery 预习视频

    1.事件补充 <input type="button" onclick="CheckAll('#edit_mode','#tb1');" value=&q ...

  5. Linux添加新硬盘自动挂载硬盘

    Linux添加新硬盘自动挂载硬盘的具体步骤 1.插入新硬盘,启动Linux服务器,使用fdisk -l 查看硬盘 #fdisk -l Disk /dev/sdb: 1000.2 GB, 1000204 ...

  6. Web Service 接口实现大量数据传输的解决方案

    1,流程图 2,流程说明:线程1开始请求接口获取1W条数据,当数据成功获取后,接口是闲置的,这时我们开始线程2获取数据,同时线程1继续执行获取数据的后续工作,如果转换数据,这里我用的办法是,使用预先定 ...

  7. 简明Vim练级攻略(转载)

    前言 今天看到这篇文章,共鸣点非常多.它把Vim使用分为4个级别,目前我自己是熟练运用前面三级的命令,在培养习惯使用第四级.完全就是我这一年来坚持使用Vim的过程.所以不管怎么我要转载这篇文章.翻译自 ...

  8. (转)Thread.setDaemon设置说明

    本想搜下python多线程里的setDaemon,发现了这篇文章写得很不错:http://blog.csdn.net/m13666368773/article/details/7245570 Thre ...

  9. VirtualBox的网络配置,Host Only+NAT方式 (zhuan)

    http://blog.csdn.net/xinghun_4/article/details/7969894 ***************************************** 其实网 ...

  10. UEditor 之查询当前编辑区域的状态是源码模式还是可视化模式

    在使用百度的编辑器的时候,遇到了这样的一个问题: 解决方法是 使用了两个命令: