题目:

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

Example:

Input: 

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 Output: 4

分析:

给定一个二维数组,其中的元素是0或者1,求最大的正方形的面积(1的个数),正方形是指内部全为1的区域。

定义一个新的二维数组dp,dp[i][j]表示以(i, j)元素为右下角的最大正方形的边长。dp[i][j]受到dp[i-1][j],dp[i][j-1],dp[i-1][j-1]影响,以下面为例说明。

1 1 1   1 1 1  
1 1 1   1 2 2
0 1 1   0 1 ?

左侧是所给的二维数组matrix,右侧是我们求得的dp数组,此时我们要求dp[2][2],也就是以matrix[2][2]为右下角的最大正方形的边长,实际上它收到左方上方和左上方的值影响,因为左侧元素上侧元素和左上侧元素同样分别是以他们各自为右下角元素的最大正方形边长,而如果当前位置的元素为1的话,意味着这个元素可以和那三个元素拼接成更大的正方形,所以在他们中选取最小的值加1,便是dp[2][2]的值,?处应填2,实际上:

if(matrix[i][j] == '1')
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;

程序:

C++

class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty())
return 0;
int m = matrix.size();
int n = matrix[0].size();
vector<vector<int>> dp(m, vector<int>(n, 0));
int result = 0;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(matrix[i][j] == '0')
continue;
if(i == 0 || j == 0){
dp[i][j] = matrix[i][j] - '0';
}
else{
dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
}
result = max(dp[i][j] * dp[i][j], result);
}
}
return result;
}
};

Java

class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix.length == 0 || matrix == null)
return 0;
int m = matrix.length;
int n = matrix[0].length;
int[][] dp = new int[m][n];
int result = 0;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(matrix[i][j] == '0')
continue;
if(i == 0 || j == 0){
dp[i][j] = matrix[i][j] - '0';
}
else{
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
}
result = Math.max(dp[i][j] * dp[i][j], result);
}
}
return result;
}
}

LeetCode 221. Maximal Square 最大正方形(C++/Java)的更多相关文章

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

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

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

  3. Java for 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 ...

  4. 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 ...

  5. (medium)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 ...

  6. Leetcode 221. Maximal Square

    本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...

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

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

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

  9. 【刷题-LeetCode】221. Maximal Square

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

  10. 【LeetCode】221. Maximal Square

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

随机推荐

  1. BURP保存多个监听器配置

    "感谢您阅读本篇博客!如果您觉得本文对您有所帮助或启发,请不吝点赞和分享给更多的朋友.您的支持是我持续创作的动力,也欢迎留言交流,让我们一起探讨技术,共同成长!谢谢!" 前言 在进 ...

  2. 【笔记】Oracle union all&for update锁

    [笔记]Oracle union all&for update union all 在Oracle中有三种类型的集合操作 UNION:求并,重复记录只显示一次 UNION ALL:求并集,显示 ...

  3. 【编程】C++ 常用容器以及一些应用案例

    介绍一些我常用的C++容器和使用方法,以及使用案例.blog 1 概述 容器(Container)是一个存储其他对象集合的持有者对象.容器以类模板实现,对支持的元素类型有很大的灵活性.容器管理元素的存 ...

  4. 力扣183(MySQL)-从不订购的客户(简单)

    题目: 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: Orders 表:  解题思路: 需要查询出没 ...

  5. 力扣326(java)-3的幂(简单)

    题目: 给定一个整数,写一个函数来判断它是否是 3 的幂次方.如果是,返回 true :否则,返回 false . 整数 n 是 3 的幂次方需满足:存在整数 x 使得 n == 3x 示例 1: 输 ...

  6. 一位 sealer maintainer 的心路历程

    简介: 本文作者将回顾个人参与 sealer 开源项目的机缘巧合,参与过程中的挑战,以及从中获取的所悟所感,写下一段文字进行分享,希望对开源新人有所帮助,能够激励想参与开源工作但还未踏出第一步的同学. ...

  7. 汽车之家:基于 Flink + Iceberg 的湖仓一体架构实践

    简介: 由汽车之家实时计算平台负责人邸星星在 4 月 17 日上海站 Meetup 分享的,基于 Flink + Iceberg 的湖仓一体架构实践. 内容简要: 一.数据仓库架构升级的背景 二.基于 ...

  8. Dubbo3.0|阿里巴巴服务框架三位一体的选择与实践

    ​简介: 服务框架就像铁路的铁轨一样,是互通的基础,只有解决了服务框架的互通,才有可能完成更高层的业务互通,所以用相同的标准统一,合二为一并共建新一代的服务框架是必然趋势.Dubbo3.0 是 Dub ...

  9. [FAQ][Hardhat] Error HH501: Couldn't download compiler version 0.8.0. Please check your connection.

    当使用 npx hardhat compile 命令编译智能合约时,会先下载你在 hardhat.config.js 配置中对应版本的 solidity 编译器. 当网络不可达时,就会提示无法下载的错 ...

  10. 【OpenVINO™】基于 C# 和 OpenVINO™ 部署 Blazeface 模型实现人脸检测

     前言 OpenVINO C# API 是一个 OpenVINO 的 .Net wrapper,应用最新的 OpenVINO 库开发,通过 OpenVINO C API 实现 .Net 对 OpenV ...