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. Formatting Excel File Using Ole2 In Oracle Forms

    Below is the some useful commands of Ole2 to format excel file in Oracle Forms.-- Change font size a ...

  3. Android 杂记 - 存货盘点用的客户端

    最近有个盘点用的东西,要放到移动设备,本来用 .Net Compact Framework,CAB 部署在 CE 系统的移动条码设备.技术太旧,我用了这个周末两天时间,把这东西在试试实现在安卓上面,给 ...

  4. Socket&GCDAsyncSocket(异步Socket)

    Socket ********************************************* 简单理解Socket 就是网络连接,可以实现两个点之间的数据通讯. •Socket允许使用长连 ...

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

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

  6. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  7. Android SDK 更新镜像服务器

    搞个新的电脑,新环境下,SDK总是更新不成功.找了一下,发现有国内的镜像,记录保存一下. Android Tools Android SDK在线更新镜像服务器    中国科学院开源协会镜像站地址:   ...

  8. DB层面上的设计 分库分表 读写分离 集群化 负载均衡

    第1章  引言 随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题.对于一个大型的 互联网应用,每天几十亿的PV无疑对数据库造成了相当高的负载.对于系统的稳定性和扩展性造成了极大的 ...

  9. HDU 1698

    成段更新 这是一种把 num[]上空结点当做lazy标志使用的方法 都一样... #include <stdio.h> #include <string.h> #include ...

  10. HDU1542矩形面积并

    取出纵向边按x坐标排序,在y方向上建立线段树. 每次查询当前有效长度len,ans += len*(x[i]-x[i-1]); 其中len为T[rt].len; 查询完毕后更新y方向上线段树,入边+1 ...