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. MeshLab中进行点云配准

    MeshLab是一个开源.可移植和可扩展的三维几何处理系统,主要用于交互处理和非结构化编辑三维三角形网格.它支持多种文件格式: import:PLY, STL, OFF, OBJ, 3DS, COLL ...

  2. IIS网站部署注意点

    在IIS上部署网站时,除了在添加网站时配置好相关程序池,主目录,安全性,选择.Netframwork版本这些步骤外, 容易忘记的是有些网站需要打开web服务扩展.

  3. plot bar chart using python

    Example import matplotlib.pyplot as plt import plotly.plotly as py # Learn about API authentication ...

  4. monkeyrunner自动登录脚本

    自己写了个平时测试的app的自动登录脚本,亲测可运行.读者参照时只需要改包名.activity名称.坐标值.账号和密码即可 查看坐标是多少的方法:使用手机的指针位置来实现:系统设置---开发者选项-- ...

  5. JS学习笔记(五) HTML DOM

    参考资料: 1. http://www.w3school.com.cn/js/js_htmldom.asp 2. http://www.runoob.com/htmldom/htmldom-tutor ...

  6. Maven生命周期(插件)

    maven拥有三套相互独立的生命周期,它们分别是clean,default和site.clean生命周期的目的是清理项目,default生命周期的目的是构建项目,而site 生命周期的目的是建立项目站 ...

  7. typeof instanceof

    typeof用以获取一个变量的类型,typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefinedinstanceof用于判断一个 ...

  8. hdu5406 CRB and Apple dp+两个LIS

    题意转换为:给定n个数,求两个最长的不相交的LIS. 先说经典题一个LIS的nlogn做法.枚举当前数,若比末尾数大,插入末尾,否则二分查找,插入合适位置. 通过此题,我们有了一个用树状数组或线段树+ ...

  9. 设计js通用库

    设计js通用库的四个步骤: 1.需求分析:分析库需要完成的所有功能. 2.编程接口:根据需求设计需要用到的接口及参数.返回值. 3.调用方法:支持链式调用,我们期望以动词方式描述接口. (ps:设计链 ...

  10. JSON入门实例

    json和XML很像,但它具有更快,更小,阅读性强等优点.不多说,直接来例子: <html><body><h2>通过 JSON 字符串来创建对象</h3> ...