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

解法一: 时间复杂度 O(nm logm),空间O(m)

将二维化为一维。累计1的高度,在每次累计时,求最值。

转化为一维后。用两个数组,分别存放当下标为j时,大于等于当前高度的最左的下标值,以及

大于等于当前高度的最右的下标值。

/**
 * @author          johnsondu
 * @problem         Maximal Square
 * @time            O(nm logm)
 * @space           O(n)
 * @url             https://leetcode.com/problems/maximal-square/
 * @strategy        dynamic programs.
 * @status          Accepted,  runtime beats 18.90% of cpp submissions. 16ms
 * @time            15:25, Nov 4th 2015
 */

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        int row = matrix.size();
        if(row < 1) return 0;
        int col = matrix[0].size();
        int ans = 0;

        vector<int> dp(col+2, 0);
        vector<int> lidx(col+1, 0);
        vector<int> ridx(col+1, 0);
        dp[0] = -1;
        dp[col + 1] = col+1;

        for(int i = 0; i < row; i ++) {
            // 将二维,转化为一维,每次累计高度。转换成
            // hdu 1506的做法。
            for(int j = 0; j < col; j ++) {
                if(matrix[i][j] == '1') dp[j+1] = dp[j+1] + 1;
                else dp[j+1] = 0;
            }

            for(int j = 1; j <= col; j ++) lidx[j] = ridx[j] = j;

            // 找到当前每个下标的至少和当前高度持平的最左下标值
            for(int j = 1; j <= col; j ++) {
                // 如果当前高度小于等于t时的当前高度,那么
                //  当前高度一定也小于等于下标t的最左的大于等于下标t的高度。
                int t = j-1;
                while(dp[j] <= dp[t] && t > 0) {
                    t = lidx[t];
                }
                lidx[j] = t;
            }
            // 找到当前每个下标的至少和当前高度持平的最右下标值
            for(int j = col; j > 0; j --) {
                // 同理
                int t = j+1;
                while(dp[j] <= dp[t] && t <= col) {
                    t = ridx[t];
                }
                ridx[j] = t;
            }
            // 求最大值
            for(int j = 1; j <= col; j ++) {
                ans = max(ans, min(ridx[j] - lidx[j] - 1, dp[j]));
            }
        }
        return ans * ans;
    }
};


解法二: 时间复杂度O(nm), 空间复杂度O(nm)

解法如下:

1) Construct a sum matrix S[R][C] for the given M[R][C].
     a) Copy first row and first columns as it is from M[][] to S[][]
     b) For other entries, use following expressions to construct S[][]
         If M[i][j] is 1 then
            S[i][j] = min(S[i][j-1], S[i-1][j], S[i-1][j-1]) + 1
         Else /*If M[i][j] is 0*/
            S[i][j] = 0
2) Find the maximum entry in S[R][C]
3) Using the value and coordinates of maximum entry in S[i], print 
   sub-matrix of M[][]


/**
 * @author          johnsondu
 * @problem         Maximal Square
 * @time            O(nm)
 * @space           O(nm)
 * @url             https://leetcode.com/problems/maximal-square/
 * @strategy        dynamic programs.
 * @status          Accepted,  runtime beats 30.20% of cpp submissions. 12ms
 * @time            15:55, Nov 4th 2015
 */

class Solution {
public:
    int maximalSquare(vector<vector<char>>& matrix) {
        int row = matrix.size();
        if(row < 1) return 0;
        int col = matrix[0].size();
        int ans = 0;

        vector<vector<int>> dp(row, vector<int>(col, 0));
        for(int i = 0; i < row; i ++)
            if(matrix[i][0] == '1') dp[i][0] = 1;
        for(int i = 0; i < col; i ++)
            if(matrix[0][i] == '1') dp[0][i] = 1;

        for(int i = 1; i < row; i ++)
        for(int j = 1; j < col; j ++) {
            if(matrix[i][j] == '1')
                dp[i][j] = min(min(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1;
            else dp[i][j] = 0;
        }

        for(int i = 0; i < row; i ++)
            for(int j = 0; j < col; j ++)
                ans = max(ans, dp[i][j]);
        return ans * ans;
    }
};


保姆级教程:用MATLAB搞定运动模糊图片修复(附完整代码与避坑指南) 本文提供了一份详细的MATLAB运动模糊图片修复教程,涵盖盲复原、图像复原等核心技术。通过傅里叶变换和Radon变换分析模糊参数,对比逆滤波、维纳滤波和Lucy-Richardson算法效果,并附完整代码与避坑指南,帮助用户实现专业级图像处理。 阅读详情

相关推荐

Unity热更实战:用YooAsset+HybridCLR实现小游戏资源与代码双更新(含避坑清单)

本文详细介绍了Unity小游戏开发中如何通过YooAsset与HybridCLR实现资源与代码双热更新方案。内容涵盖环境配置、架构设计、微信小游戏专项优化、热更代码工程化实践及真机调试技巧,帮助开发者解决审核周期长、紧急修复等痛点,提升更新效率与稳定性。

weixin_42520025的博客 501

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

C#实战:基于ONNX与SCRFD/ArcFace的高效人脸识别系统开发

本文详细介绍了如何在C#环境中,利用ONNX Runtime高效部署SCRFD人脸检测与ArcFace人脸识别模型,构建一个完整的、高性能的人脸识别系统。内容涵盖环境搭建、模型推理、人脸对齐、特征提取等核心模块,并提供了从Demo到生产系统的优化思路与实战代码,为.NET开发者提供了一套开箱即用的解决方案。

weixin_29061821的博客 488

leetcode 221: Maximal Square

leetcode 221: Maximal Square c++ python java

西施豆腐渣 1万+

VS2022+Qt6保姆级环境配置指南:从插件安装到项目互通全流程

本文提供VS2022与Qt6深度整合的全流程指南,涵盖环境配置、插件安装、项目互通等关键步骤。详细解析编译器版本匹配、跨IDE协作方案及效能优化技巧,帮助开发者高效搭建C++界面设计开发环境,实现软件项目无缝互通。

zero1的博客 731

看不懂分子动力学模拟的结果?一文读懂RMSD曲线

随后,将每一帧结构与初始模板进行叠合,计算对应原子的平均偏移量,即RMSD。总之,掌握 RMSD 的解读方法,就等于抓住了评估分子动力学模拟成败与质量的“总开关”。①在模拟初始阶段,RMSD会从0迅速上升,这是蛋白质从静态晶体或模型结构释放到溶液环境中并逐渐适应的正常过程。②经过一段时间后,曲线不再系统性上升或下降,而是围绕一个平均值,小幅度地上下波动,形成一个稳定的“平台”。这种情况下,RMSD 在初始快速上升后,并未进入平台,而是持续甚至加速上升,始终未见收敛。🔍 如何解读不同趋势的RMSD曲线。

weixin_46587777的博客 4884

LeetCode 221. Maximal Square 题解

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

cannonjinx的博客 4109

使用Termux在Android设备上编译运行SpecCPU2006

SPEC2006搭配Termux,竟然还能这么玩?!

菜菜的菜园子 2958

Leetcode 221. Maximal Square

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

Za七杂⑧ 169

电子元件及其规格书的查找与理解

本内容收录:在电子开发或维修过程中,查找电子元件及其规格书心得体会,以及元件规格书的理解,系个人多年实战经验的系统梳理。内容着重介绍贴片元件代码反查型号的实用技巧,并以三款典型疑难元件为例,详述其完整查找过程与最终型号确认。

让你爱上电路设计 3985

学Simulink——基于大数据处理与云计算的场景实例:无人机与云计算中心之间的高效数据传输链路设计

本文以无人机与云计算中心的高效数据传输链路设计为例,手把手指导使用Simulink进行系统级建模。针对智慧城市、灾害监测等场景中无人机采集的海量数据(视频、图像、点云等),构建包含数据源、传输协议、无线链路、云接入和性能分析的五层架构模型。通过智能数据压缩、多链路聚合和QoS优先级调度等关键技术,实现5G+Wi-Fi等多通信技术的自适应切换。仿真结果表明,5G+Wi-Fi链路聚合在城市环境中表现最优,而4G+卫星组合适合偏远地区。该模型为无人机实时数据传输提供了有效的设计方法和优化方向,可拓展至边缘计算、A

xiaoheshang_123的博客 120

[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

YOLOv11改进策略【卷积层】| CVPR-2024 PKI Module 获取多尺度纹理特征,适应尺度变化大的目标

PKI Module是。

Limiiiing的博客 2705

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

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

[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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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 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 R

coder 进阶的专栏 1464

[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 //C - 221. Maximal Square

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

Made in Code 1232

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 221 Maximal Square

思路 dp解法。dp[i][j]表示以(i, j)为右下角的正方形的最大边长; 状态转移方程:dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j]) + 1; 初始状态:i == 0 || j == 0, dp[i][j] = matrix[i][j]. 复杂度 时间复杂度O(n*m) = O(n^2) 空间复杂度O(n*m) = O(n^2) 代...

我要上岸的博客 173

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 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 0Ret

一只不断成长的藏獒 1357

LeetCode-221. Maximal Square [C++][Java]

Given anm x nbinarymatrixfilled with0's and1's,find the largest square containing only1'sand return its area.

贫道绝缘子的博客 180
上一篇: 【leetcode】11. Container With Most Water
下一篇: 【leetcode】119. Pascal's Triangle II
小do在努力
博客等级 码龄16年 58粉丝 161原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值