当前位置: 首页 > news >正文

LeetCode -- 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
Return 4.




在一个矩阵中,找到最大的正方形面积(1表示正方形的点,0表示空)。




本题是很典型的DP问题。


1. 把dp[0,i]赋值为matrix[0,i] , dp[i,0]赋值为matrix[i,0]。i∈[0,n)


2. 两层循环根据不同情况为dp[i,j]赋值:
a. matrix[i,j] == 1 且3个邻居(dp[i-1,j],dp[i,j-1],dp[i-1,j-1])均为1 : dp[i,j] = 4
b. matrix[i,j] == 1 且3个邻居>1且相等: dp[i,j] = (邻居面积的平方根+1)的平方
c. matrix[i,j] == 1 且3个邻居>=1但不一定相等: dp[i,j]=(邻居中最小值的平方根+1)的平方
d. 其他情况: dp[i,j] = matrix[i,j]


3.使用max变量来track当前dp[i,j]的最值


实现代码:






public class Solution {
    public int MaximalSquare(char[,] matrix) {
        
        var row = matrix.GetLength(0);
        var col = matrix.GetLength(1);
        if(row < 2){
            if(row == 0){
				return 0;
			}
			else if(col == 1){
				return matrix[0,0] == '1' ? 1 : 0;
			}
        }
        var max = 0;
		
        var dp = new int[row, col];
        for(var i = 0;i < row; i++){
            var x = matrix[i,0] == '1' ? 1 : 0;
			dp[i, 0] = x;
			
			if(dp[i,0] > max){max = dp[i,0];}
        }
        for(var i = 0;i < col; i++){
            var x = matrix[0,i] == '1' ? 1 : 0;
			dp[0, i] = x;
			if(dp[0,i] > max){max = dp[0,i];}
        }
        
        for(var i = 1;i < row; i++){
            for(var j = 1;j < col; j++){
				// neighbours all equals 1
                if(matrix[i,j] == '1' && dp[i-1,j] == 1 && dp[i, j-1] == 1 && dp[i-1,j-1] == 1){
					if(dp[i-1, j] == 1){
						dp[i,j] = 4;
					}
                }
				// neighbours all bigger than 1 and equals each other
				else if(matrix[i,j] == '1' && dp[i-1,j] == dp[i,j-1] && dp[i-1,j-1] == dp[i-1,j] && dp[i-1,j] > 1){
					dp[i,j] = (int)Math.Pow(Math.Sqrt(dp[i,j-1]) + 1,2);
				}
				// neighbours all no less than 1, but may not equals each other
				else if(matrix[i,j] == '1' && dp[i-1,j] >= 1 && dp[i,j-1] >= 1 && dp[i-1,j-1] >= 1){
					var min = Math.Min(Math.Min(dp[i-1,j-1], dp[i-1,j]), dp[i,j-1]);
					dp[i,j] = (int)Math.Pow(Math.Sqrt(min) + 1,2);
				}
                else{
                    dp[i,j] = matrix[i,j] == '1' ? 1 : 0;
                }
				
				if(dp[i,j] > max){max = dp[i,j];}
            }
        }
		
        return max;
        
    }
}


相关文章:

  • ArcGIS Server Java ADF 案例教程 18
  • LeetCode -- Summary Ranges
  • ArcGIS Server Java ADF 案例教程 19
  • ArcGIS Server Java ADF 案例教程 20
  • LeetCode -- Unique Paths
  • 警惕手机流氓软件的流行
  • LeetCode -- Combination Sum II
  • 学习百度、腾讯如何把产品做到极致(转载)
  • LeetCode -- Edit Distance
  • Leetcode -- Find Minimum in Rotated Sorted Array
  • SQL2005CLR函数扩展-树的结构
  • LeetCode -- Longest Consecutive Sequence
  • Flex与.NET互操作(八):使用FluorineFx网关实现远程访问
  • LeetCode -- Missing Number
  • [Windows编程] Windows 7 对多核的支持
  • IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
  • 10个确保微服务与容器安全的最佳实践
  • 5、React组件事件详解
  • CentOS6 编译安装 redis-3.2.3
  • Effective Java 笔记(一)
  • idea + plantuml 画流程图
  • iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
  • Spark学习笔记之相关记录
  • SpiderData 2019年2月16日 DApp数据排行榜
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 和 || 运算
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 模型微调
  • 如何设计一个比特币钱包服务
  • 首页查询功能的一次实现过程
  • 数据仓库的几种建模方法
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • ​水经微图Web1.5.0版即将上线
  • ​学习一下,什么是预包装食品?​
  • #100天计划# 2013年9月29日
  • #etcd#安装时出错
  • (1)虚拟机的安装与使用,linux系统安装
  • (2009.11版)《网络管理员考试 考前冲刺预测卷及考点解析》复习重点
  • (2020)Java后端开发----(面试题和笔试题)
  • (poj1.2.1)1970(筛选法模拟)
  • (附源码)springboot 智能停车场系统 毕业设计065415
  • (七)理解angular中的module和injector,即依赖注入
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (万字长文)Spring的核心知识尽揽其中
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (学习日记)2024.02.29:UCOSIII第二节
  • (转) 深度模型优化性能 调参
  • **PyTorch月学习计划 - 第一周;第6-7天: 自动梯度(Autograd)**
  • ./和../以及/和~之间的区别
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .NET Core使用NPOI导出复杂,美观的Excel详解
  • .Net环境下的缓存技术介绍
  • @NoArgsConstructor和@AllArgsConstructor,@Builder
  • @vue/cli 3.x+引入jQuery
  • [2023年]-hadoop面试真题(一)