int maximalSquare(vector<vector<char>>& matrix) {
int height=matrix.size();
if(height==)
return ;
int width=matrix[].size();
vector<vector<int>> vec(height,vector<int>(width,));
int result=;
for(int i=;i<height;i++)
{
for(int j=;j<width;j++)
{
if(matrix[i][j]=='')
{
vec[i][j]=;
if(i>&&j>)
vec[i][j]+=min(min(vec[i-][j],vec[i][j-]),vec[i-][j-]);
}
result=max(result,vec[i][j]);
}
}
return result*result; }

本题是动态规划的题目,vec[i,j]记录的是以matrix[i,j]为右下角的所能构成的正方形区域的边长的最大值。

递推公式,vec[i][j] = 1 + min(min(vec[i-1][j],vec[i][j-1]),vec[i-1][j-1]),计算的是当前单元格的“左”、“上”、“左上”三个单元格的最小值,再+1。

补充一个python的实现:注意i和j的值在matrix和dp中表示位置不同。

 class Solution:
def maximalSquare(self, matrix: 'List[List[str]]') -> 'int':
m = len(matrix)
if m == :
return
n = len(matrix[])
dp = [[ for _ in range(n+)]for _ in range(m+)]
res =
for i in range(m):
for j in range(n):
if matrix[i][j] == '':
dp[i+][j+] = min(min(dp[i][j+],dp[i+][j]),dp[i][j]) +
res = max(res,dp[i+][j+])
return res * res

例如:对于输入测试样本,当遍历到红色的数字1时(i=2,j=3)

1 0 1 0 0
1 0 1 1 1
1 1 1 1
1 0 0 1 0

当i=2  j=3时,dp[3][4]的值是:dp[2][3]、dp[2][4]、dp[3][3]这三个位置的最小值1,再加上1。因此dp[3][4] = 1 + 1 = 2(即下图中的红色方框区域的计算)。

按照次递推公式计算,可得到如下二维数组dp:

leetcode221的更多相关文章

  1. [Swift]LeetCode221. 最大正方形 | Maximal Square

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

  2. [Leetcode221]最大面积 Maximal Square

    [题目] Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...

  3. leetcode221 Maximal Square

    思路: dp. 实现: class Solution { public: int maximalSquare(vector<vector<char>>& matrix) ...

  4. Leetcode221. Maximal Square最大正方形

    在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 方法一 ...

  5. 我熬夜读完这份“高分宝典”,竟4面拿下字节跳动offer

    前言 怎样的契机? 实际上,目前毕业已经两年时间了,在大学时就已经开始关注字节跳动的发展.一开始,我是电气自动化专业的,大二清楚目标之后就转计算机了,大四进了一家小型的互联网公司实习,具体就不说哪家了 ...

随机推荐

  1. keil项目的调试与编译

    编译: Translate===编译单个文件 Build====编译当前项目,如果该项目先前编译过1次,并且文件没有编辑改动,则点击时不会重新编译 Rebuild===重新编译,每点击一次就重新编译. ...

  2. 一条命令将windows下多个ts文件合并为一个ts文件

    首先在待合并的文件夹下创建concat.bat(名字随意啦),写入如下命令 copy /b "%~dp0"\*.ts "%~dp0"\new.ts 执行该命令后 ...

  3. Spring3(一) 控制反转(IoC)和依赖注入(DI)

    几个常用框架之间的关系 1       spring框架概述 1.1   什么是spring Spring是一个开源.轻量级的Java 开发框架.框架的主要优势之一就是其分层架构,分层架构允许使用者选 ...

  4. [LeetCode&Python] Problem 458. Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  5. 利用maven的profiles灵活的配置多环境

    <!--多环境配置--> <profiles> <profile> <id>dev</id> <activation> < ...

  6. Unreal Engine 4 C++ UCLASS构造函数易出错分析

    Unreal Engine 4 C UCLASS构造函数易出错分析 GENERATED_BODY GENERATED_UCLASS_BODY 在Unreal Engine 4的任意类中通常会见到两个宏 ...

  7. poj2228 Naptime【(环结构)线性DP】

    Naptime Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:3374   Accepted: 1281 Descriptio ...

  8. 【SpringBoot】Logback日志框架介绍和SpringBoot整合实战

    ========================11.Logback日志框架介绍和SpringBoot整合实战 2节课================================ 1.新日志框架L ...

  9. jenkins 结合 jmeter 的报告篇

    背景:实现jmeter每30分钟执行一次,运行系统中所有接口或者性能脚本或者后续更可以由系统部署来触发建构执行 1.配置jmeter测试环境,注意修改Jmeter的bin目录下jmeter.prope ...

  10. day 41 标准文档流 浮动

    一.标准文档流 什么是标准文档流 宏观的将,我们的web页面和ps等设计软件有本质的区别,web 网页的制作,是个“流”,从上而下 ,像 “织毛衣”.而设计软件 ,想往哪里画东西,就去哪里画 标准文档 ...