题目:

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. 力扣180(MySQL)-连续出现的数字(中等)

    题目: 编写一个 SQL 查询,查找所有至少连续出现三次的数字. 返回的结果表中的数据可以按 任意顺序 排列. 查询结果格式如下面的例子所示: 解题思路: 原表数据: 方法一: 使用内连接(inner ...

  2. 阿里云拨测:主动探测Web应用质量,助力提升用户体验

    简介: 阿里云拨测是一种针对互联网应用(Web页面.网络链路等)进行应用性能和用户体验监测的服务,无需嵌码即可为云上用户提供开箱即用的企业级主动拨测式应用监测解决方案. 随着中国数字化经济的蓬勃发展, ...

  3. 媒体智能-淘宝直播流媒体互动实践 | D2 分享视频+文章

    背景:今天给大家带来的分享主题是<媒体智能-淘宝直播流媒体互动实践>,内容分为5个部分,首先看看在淘宝直播的直播间里主播可以怎样给用户拜年:然后具体讲如何制作一个手势拜年的特效:接着介绍我 ...

  4. Docker Desktop v20.10.8 和 WSL2 迁移镜像存储目录

    只迁移存储镜像和挂载文件目录:https://www.cnblogs.com/lemonK/p/17781775.html 同时迁移docker程序目录:https://www.cnblogs.com ...

  5. [PHP] Laravel 的 503 Service Unavailable 模板提示的来源

    当我们看到 Laravel 的 503 样式的模板时,是启用维护模式的时候的提示(php artisan down). 开启访问运行 php artisan up. Refer:Laravel 503 ...

  6. [FE] JS 判断当前是否在微信浏览器中的最新代码

    注意以下使用了 const 定义未改变的变量,没有使用 var. function isWeChatBrowser () { const ua = window.navigator.userAgent ...

  7. 在 VisualStudio 给文件起一个带分号的文件名会怎样

    小伙伴都知道在 Windows 下是支持文件名使用分号的,而写过 Roslyn 的小伙伴都知道,在 csproj 项目里面使用分号分割数组.那么在 VS 里面将一个文件名添加分号会如何?下面让咱写写看 ...

  8. OSI模型之数据链路层

    一.简介 数据链路层在物理层提供服务的基础上向网络层提供服务,其最基本的服务是将源自网络层的数据可靠地传输到相邻节点的目标机网络层.其主要作用是加强物理层传输原始比特流的功能,将物理层提供的可能出错的 ...

  9. python 操作 xlsx

    目录 读取/写入:openpyxl demo1 读取/写入:openpyxl demo1 import openpyxl import os # 创建excel def write_excel_xls ...

  10. 基于 three.js 加载器分别加载模型

    点击查看代码 /** * 参数:模型文件路径,成功回调函数 * * 基于 three.js 加载器分别加载模型 * * 全部加载后通过回调函数传出打印 */ import { FBXLoader } ...