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. An Example of Pre-Query and Post-Query Triggers in Oracle Forms With Using Display_Item to Highlight Dynamically

    Example is given for Pre-Query and Post-Query triggers in Oracle Forms, with using Display_Itembuilt ...

  2. 图解SQL 2008数据库复制

    为了达到数据及时备份,一般采用完整备份+差异备份即可,或者再加上日志备份,本文介绍使用数据库复制技术同步数据: PS:文章以图片为主,图片更能直观的看出操作步骤和配置方法! 1.首先创建一个测试的数据 ...

  3. CUBRID学习笔记 18 sql语句的预处理(类似存储过程)

    定义预处理  类似sqlserver的存储过程 语法 PREPARE stmt_name FROM preparable_stmt 说明 PREPARE 关键字 stmt_name 预处理语句的名字 ...

  4. 6.mybatis异常:SQL Mapper Configuration,Error parsing Mapper XML,Could not resolve type alias

    在xxxMapper中 <select id="getClazz" parameterType="int" resultType="getCla ...

  5. 关于deferred

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 关于Ubuntu中passwd、shadow、group等文件

    在Ubuntu系统中,/etc目录下,有三个文件:passwd shadow group,可能我们已经在用了,但是没有注意到其详细. 这三个配置文件用于系统帐号管理,都是文本文件,可用vi等文本编辑器 ...

  7. phpcms 头部搜索栏上边的 “新闻 | 图片 | 下载 | 专题” 是在哪里修改的?

    phpcms 是怎么修改以下 栏目列. 在后台的时候,“模块管理>全站搜索”,可以修改搜索分类

  8. Android中向SD卡读写数据,读SD卡和手机内存

    package com.example.sdoperation; import java.io.BufferedReader; import java.io.File; import java.io. ...

  9. Android Fast ImageLoader

    前段时间写的Android平台开源项目:Fast ImageLoader,现在分享给大家 源码地址:https://github.com/cumtkangyi/Android-Fast-ImageLo ...

  10. 《易货》Alpha版本测试报告

    一.测试计划 功能需求编号 功能需求名称 功能需求描述 测试计划 1 用户注册 每一个想要发布商品或者需要购买商品的用户都需要注册一个账号 √ 2 用户登录 已经拥有账号的用户登录 √ 3 密码修改 ...