[LeetCode 221] Maximal Square

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 阅读详情

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 following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.


solution:

DP, f(x,y) represents length of the right-bottom node of square,

ifmatrix[i][j] == 1, f(x,y) = Math.min(f(x-1,y), f(x,y-1),f(x-1,y-1))+1;


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


给大一新生的蓝桥杯EDA备赛规划:从电路分析到立创EDA,如何用一年时间高效备赛? 本文为大一新生提供了一份系统的蓝桥杯EDA备赛规划,从电路分析基础到立创EDA实战操作,分阶段指导如何高效备赛。内容涵盖理论学习、软件操作、PCB设计专项提升及冲刺优化策略,帮助学生在一年内全面提升电子设计能力,为竞赛做好充分准备。 阅读详情

相关推荐

字节开源 DeerFlow 2.0:从 Deep Research 到 Super Agent Harness 的进化之路

GitHub Trending 冠军项目,字节跳动完全开源的超级智能体框架。支持子代理、沙箱执行、长期记忆、技能扩展,可完成从几分钟到几小时的复杂任务。本文深度解析架构设计、安装实战、技能系统,并附上独家配置指南。

qq_38610923的博客 1712

python leetcode 221. Maximal Square

class Solution(object): def maximalSquare(self, matrix): """ :type matrix: List[List[str]] :rtype: int """ #dp[i][j]存储的是右下角坐标[i,j]的最大边长 if matrix =...

Neekity的博客 650

《iOS之一起进大厂》系列-秒杀面试官的RunLoop底层原理面试题

Runloop是iOS面试中的必考题,不管大厂小厂都会问到。 这里包括了Runloop几乎所有的面试题,还有中肯完整的答案解释。

空杯子 1152

【LeetCode】221. Maximal Square 解题报告(Python & C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal-square/description/ 题目描述: Given a 2D binary matrix filled with 0’s and 1’s, find the largest s...

负雪明烛 5706

Weaxor勒索病毒实战分析:从加密算法到防御策略(附检测工具清单)

本文对Weaxor勒索病毒进行了深度实战剖析,详细拆解了其采用ChaCha20与AES-CTR的混合加密算法、横向移动手法及数据回传行为。文章重点分析了其密钥生成机制的“漏洞修复”,并提供了从主机加固、网络监控到基于IOC威胁狩猎的立体化企业级防御策略与检测工具清单。

weixin_29233333的博客 262

博途WinCC上位机避坑指南:从工控机选型到趋势图优化的完整流程

本文详细解析了博途WinCC上位机开发全流程,从工控机选型到趋势图优化的关键技巧。针对工业自动化场景,提供硬件配置指南、网络优化方案和性能提升策略,帮助工程师避开常见陷阱,实现稳定高效的HMI系统部署。特别强调PC Station与传统触摸屏的选型对比及SIMATIC NET配置优化。

pp12345的博客 278

leetcode 221: Maximal Square

leetcode 221: Maximal Square c++ python java

西施豆腐渣 1万+

深入解析UE5内存分析:从Memreport到Unreal Insights

本文深入解析了UE5内存分析的两大核心工具:Memreport与Unreal Insights。Memreport提供详细的内存快照,帮助定位UObject等资源的内存占用;而Unreal Insights则能进行实时监控与历史回溯,精准追踪内存泄漏。掌握这两款工具是优化UE5项目性能、确保大型项目稳定运行的必备技能。

lg888的博客 1007

LeetCode 221. 最大正方形 | Python

使用动态规划,找出最长边,进而解决《221. 最大正方形》问题

"大梦三千秋的博客 1289

ArcMap实战:用栅格计算器搞定16年NDVI趋势分析,附完整F检验流程

本文详细介绍了如何使用ArcMap的栅格计算器进行16年NDVI趋势分析,包括数据预处理、线性趋势计算和F显著性检验的全流程操作。通过实际案例和统计原理解析,帮助GIS从业者掌握植被覆盖变化的分析方法,特别适合生态评估和农业监测应用。

weixin_29251567的博客 252

从零到精通:ROS2 MoveIt2与MPC机械臂轨迹规划的完整配置流程

本文详细介绍了ROS2 MoveIt2与MPC机械臂轨迹规划的完整配置流程,从环境准备到高级功能扩展,帮助开发者实现精准控制。通过集成模型预测控制(MPC),机械臂能够优化复杂轨迹规划,处理动态避障,提升工业自动化效率。

juice的博客 706

[leetcode] 221. Maximal Square @ python

原题 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 解法 参考: 花花酱 LeetC...

闲庭信步 386

VMware下载与安装

一、VMware下载 二、VMware安装

欢迎订阅我的“JAVA全栈开发笔记(全)”专栏,价格优惠,仅需19.9元。 315

和我一起刷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

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

LeetCode 221. Maximal Square 题解

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

cannonjinx的博客 4109

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

Leetcode 221. Maximal Square

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

Za七杂⑧ 169

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[leetcode 221]Maximal Square

Maximal Square Total Accepted: 8699 Total Submissions: 44034 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,

ER_Plough的专栏 660

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: 给出一个由0,1构成的二维数组,让找出里面最大的正方形的面积 思路: 因为是正方形,长和宽是一样的,较短的一方决定了正方形的边长 假如给一个input...

蓝羽飞鸟的博客 201

LeetCode 221. Maximal Square

题目链接:最大正方形 - 力扣 (LeetCode) 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 ...

zhaohaibo的博客 382

Leetcode221. Maximal Square

在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。

magic_jiayu的博客 256

[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这题其实很明显是一道dp题来的。我猜当...

chaochen1407的专栏 203

leetcode221——Maximal Square

题目大意:在一个由0和1组成的二维矩阵内,找到只包含1的最大正方形,并返回其面积。 分析:动态规划。 dp[i][j]——以matrix[i - 1][j - 1]为正方形右下角时的最大正方形边长 初始化:dp[i][j] = 0 状态转换方程:如果matrix[i - 1][j - 1] == ‘1’,更新dp[i][j] =min(dp[i - 1][j - 1],min(dp[i]...

tzyshiwolaogongya的博客 205

LeetCode221: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 following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0

3667

LeetCode //C - 221. Maximal Square

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

Made in Code 1232

[LeetCode]221. Maximal Square

[LeetCode]221. Maximal Square题目描述思路一种思路是直方图做法,可以解扩展到矩形的情况,见另一篇博客 [LeetCode]85. Maximal Rectangle另外的思路是动态规划解法给出的样例输入为 matrix 0 1 2 3 4 0 1 0 1 0 0 1 1 0 1 1 1 2

Lcharon的博客 391
上一篇: [LeetCode 238] Product of Array Except Self
下一篇: [LeetCode 273] Integer to English Word
Mavs
博客等级 码龄17年 166粉丝 296原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值