Leetcode 221 - Maximal Square(dp)

Leetcode—221.Maximal Square 最大正方形 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 阅读详情

题意

给定一个由01组成的矩形,要求找出矩形内由1组成的最大正方形面积。

思路

状态表示:d[i,j],以位置为矩形右下角的最大正方形的边长。r[i,j],第i行,到第j个位置的1的长度。c[i,j],第j列,到第i个位置的1的长度。

转移方程:d[i,j]=min{d[i−1,j−1]+1,r[i,j],c[i,j]}

时间复杂度:O(n2)

代码

const int maxn = 505;
class Solution {
public:
    int d[maxn][maxn], r[maxn][maxn], c[maxn][maxn];
    int maximalSquare(vector<vector<char>>& matrix) {
        int m = matrix.size();
        if (m) {
            int n = matrix[0].size(), ans = 0;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (matrix[i][j] == '1') {
                        r[i][j] = (j ? r[i][j - 1] + 1 : 1);
                        c[i][j] = (i ? c[i - 1][j] + 1 : 1);
                    } else {
                        r[i][j] = c[i][j] = 0;
                    }
                    d[i][j] = min(min(r[i][j], c[i][j]), i && j ? d[i - 1][j - 1] + 1 : 1);
                    ans = max(ans, d[i][j]);
                }
            }
            return ans * ans;
        }
        return 0;
    }
};
LIN协议栈 AUTOSAR架构下 状态管理 LIN State Manager 通过对这些状态的管理和转换,实现了对 LIN 总线通信状态的精确控制,以满足汽车电子系统中不同场景的需求。 阅读详情

相关推荐

win11禁止更新,草履虫教程

本文介绍了彻底禁止Windows 11系统更新的组合方法,包括禁用核心服务、修改注册表、关闭计划任务和使用组策略(专业版适用)。作者特别提醒,禁止更新会影响系统安全性,建议实体机保持更新,仅虚拟机等特殊情况下使用这些方法。同时强调即使禁用更新,也应定期手动检查重要安全补丁。文章最后附上了作者的个人网站信息。

testwzc1的博客 561

leetcode 221. Maximal Square [dp]

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 class Solution { pu...

ljf_study的博客 184

数字IC后端手把手实战教程 | Innovus verify_drc VIA1 DRC Violation解析及脚本自动化修复方案

数字IC后端实战项目案例之Innovus verify_drc DRC Violation脚本自动化修复方案

weixin_37584728的博客 4048

(DP 线性DP) leetcode 221. Maximal Square

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

124

从TI官方手册里挖细节:TPS5430负压电路那个不起眼却救命的Cd电容

本文深入解析TPS5430负压电路中关键Cd电容的作用与设计要点,揭示其在DC-DC电源设计中的重要性。通过对比TI手册和实战案例,详细阐述Cd电容在高频电流回路、能量缓冲和噪声抑制中的核心功能,并提供选型与布局指南,帮助工程师避免芯片烧毁风险,提升电路稳定性。

weixin_27102543的博客 357

一种解决STM32多串口同时收发的方法

在做项目中,遇到了同时调用串口通信时程序崩溃的问题,在项目中,串口1用作调试串口,串口2用作MQTT通信串口,串口3用作下位机通信串口, 串口1重定向以后,用库函数自带的printf函数打印字符串 串口2使用自己写的u2_printf函数,即va_list这套变参宏定义后使用vsprintf函数打印字符串 串口3刚开始使用串口2一样的打印方式,程序在调试调用到串口3打印函数时直接崩溃,后续将串口3改用dma转存输出,仍然无法正常调用 多次研究以后,采用三个串口都使用重定向后的库函数自带的printf

Zxp0605的博客 8796

**Leetcode 221. Maximal Square | dp

https://leetcode.com/problems/maximal-square/description/参考了这里的写法http://blog.youkuaiyun.com/u012501459/article/details/46553139最暴力的想法还是蛮容易想到的,但是还是上面写的好点class Solution { public: int maximalSquare(vector&...

Pilgrim 182

潜在图神经网络学习笔记(Latent Graph Neural Networks for Human Skeleton-based Action Recognition)

在本文中,我们从一个新的角度来解决这个问题,通过将非结构化的骨架数据映射到潜在空间,如图2所示,这里称为潜在特征空间或嵌入空间,其中一组项嵌入在一个流形中[ 1 ],并且在潜在空间中彼此更相似的项被放置在彼此更接近的位置。潜在空间中的位置可以被认为是由一组潜在变量定义的,这些潜在变量来源于对象的相似性。将骨架数据嵌入到流形中,消除了远近邻居之间的冗余依赖关系,以解决加权问题。得到具有多尺度聚合特征的稀疏图矩阵结构,以更好地理解骨骼关节的直接加权信息。

qq_45631885的博客 489

[Leetcode] #221 Maximal Square (DP)

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

奔跑的小乌龟 324

学Simulink——基于新型动力与推进系统的场景实例:混合动力无人机的续航能力优化

通过本次 Simulink 仿真,你已掌握:✅混合动力系统架构设计✅内燃机、电机、电池联合建模✅三种能量管理策略(规则型、功率跟随、优化型)✅续航能力仿真与对比分析✅参数调优与优化方法核心收获混合动力是突破续航瓶颈的有效路径能量管理策略(EMS)决定系统效率电池SOC管理是关键Simulink 支持复杂多能源系统的协同仿真掌握混合动力无人机技术,你将具备设计长航时巡检机、应急通信平台、边境巡逻系统的能力。

xiaoheshang_123的博客 100

【电弧】基于matlab simulink电弧仿真模型(Mayr Cassie电弧模型)【含Matlab源码 9226期】

simulink电弧仿真模型(Mayr Cassie电弧模型) 完整代码,包运行;可提供运行操作视频!适合小白!

订阅付费专栏Matlab(奶茶价版),可赠送奶茶价版付费专栏指定代码1份; 193

LeetCode 221 Maximal Square (最大子正方形 dp)

LeetCode 221 Maximal Square (最大子正方形 dp)

Tc的专栏 1213

educoder实训——计算几何形状的表面积与体积

educoder python实训

qq_41234663的博客 1230

Leetcode-221. Maximal Square 最大正方形 (DP)

题目 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。 链接:https://leetcode.com/problems/maximal-square/ 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

JamieLuo的博客 265

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

DP问题:leetcode(221) Maximal Square

问题描述: 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

u014514939的博客 175

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

Leetcode 221. Maximal Square 单调队列和dp两种思路求解

题意 给定一个0,1矩阵,希望找到矩阵中的一个面积最大的联通正方形区域,这个区域中全是1 思路 基本思路:找面积最大的,其实就是找最长的边长,我们的思路都是以这个为出发点的 dp思路 整体来看,就是一个二维dp,是这类矩阵问题的一个常用状态设置方法:dp(i,j)dp(i,j)dp(i, j) 的含义是以(i,j)(i,j)(i, j)为右下角的最大正方形边长 然后递推...

luke2834的专栏 557

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

DP专题10 - leetcode221. Maximal Square/576. Out of Boundary Paths ★

221. Maximal Square 题目描述 给定一个2维二进制矩阵(包含0和1),找出只包含数字1的最大正方形,并返回它的面积。 例子 Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Output: 4 思想 (DP) dp[i][j]表示以当前位置(i, j)为右下角的最大面积和。 初始化:只有当前位置为‘1’,面积才可能为1 转移方程...

libh的博客 206

leetcode 221. Maximal Square 最大正方形面积 + 动态规划DP实现

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

JackZhangNJU的专栏 1911

[leetcode] 221. Maximal Square 解题报告

题目链接:https://leetcode.com/problems/maximal-square/ Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the foll

小榕流光的专栏 724

LeetCode 221. Maximal Square 题解

本题是动态规划在二维矩阵中的应用。状态定义:以 (i,j) 为右下角的最大正方形边长状态转移:考虑左、上、左上三个方向空间优化:使用一维数组通过本题可以深入理解二维动态规划问题。

cannonjinx的博客 4109

和我一起刷leetcode-221-Maximal-Square

和我一起刷leetcode-221. Maximal Square 大家好,今天我们一起做Leetcode第221题 ,这是一道难度为Medium的题目。也是一道经典的动态规划练习题 题目描述 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and retur...

老方刷题 266

LeetCode //C - 221. Maximal Square

【代码】LeetCode //C - 221. Maximal Square。

Made in Code 1232

leetcode 221: Maximal Square

leetcode 221: Maximal Square c++ python java

西施豆腐渣 1万+

Leetcode 221. Maximal Square

题目描述:找出题目中最大的1正方形。 题目链接:Leetcode 221. Maximal Square 思路:当我们判断以某个点为正方形右下角时最大的正方形时,那它的上方,左方和左上方三个点也一定是某个正方形的右下角,否则该点为右下角的正方形最大就是它自己了。这是定性的判断,那具体的最大正方形边长呢?我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形...

Za七杂⑧ 169

221. Maximal Square

221. Maximal Square Tushar &amp; discussion : https://www.youtube.com/watch?v=_Lf1looyJMU, https://leetcode.com/problems/maximal-square/discuss/61803/C%2B%2B-space-optimized-DP 多种解法的blog:http://www.cn...

wilzxu的博客 118

LeetCode 221:Maximal Square 动态规划详解

这道题的价值不只是写出一个通过的答案,更重要的是:习惯用“以谁为结尾/为右下角”来定义状态。​这里有几个可以继续思考和练习的方向:能否自己实现一维滚动数组版本,把空间压到 O(n)?​能否类比这题,去看“最大矩形(Maximal Rectangle)”、“最大全 1 子矩阵”等题?​能否把“上、左、左上取 min + 1”这个模式,总结成一种“从相邻子问题扩张目标形状”的通用套路?​。

六六哥的博客 1380
上一篇: Leetcode 139 - Word Break(线性dp)
下一篇: Leetcode 85 - Maximal Rectangle(dp)
Lzed
博客等级 码龄12年 5粉丝 256原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值