前言:这道题目我感觉确定状态反而是比较难的一件事

确定状态

设dp[i][j]代表到i行,j列的最大的正方形边长

因为某一个点的最大正方形边长实际上是受他左边的点,上面的点,以及左上角的点影响的。

我们知道,该点为右下角的正方形的最大边长,最多比它的上方,左方和左上方为右下角的正方形的边长多1,最好的情况是是它的上方,左方和左上方为右下角的正方形的大小都一样的,这样加上该点就可以构成一个更大的正方形。     但如果它的上方,左方和左上方为右下角的正方形的大小不一样,合起来就会缺了某个角落,这时候只能取那三个正方形中最小的正方形的边长加1了

状态转移方程

dp[i][j] =  0       {matrix[i][j]==0}

=  min{dp[i][j-1],dp[i-1,j],dp[i-1][j-1]}+1  {matrix[i][j]==1}

初始条件和边界条件

计算顺序

dp[0][0] dp[1][0]...dp[n][0]

....

dp[n][0] dp[n][1] ...dp[n][n]

代码

class Solution {public int maximalSquare(char[][] matrix) {if(matrix.length==0||matrix==null)return 0;int [][]dp=new int [matrix.length+1][matrix[0].length+1];int res=0;for(int i=1;i<=matrix.length;i++){for(int j=1;j<=matrix[0].length;j++){if(matrix[i-1][j-1]=='1'){dp[i][j]=Math.min(Math.min(dp[i][j-1],dp[i-1][j]),dp[i-1][j-1])+1;res=Math.max(res,dp[i][j]);}}}return res*res;}
}

动态规划实战7 leetcode-221. Maximal Square相关推荐

  1. leetcode 221. Maximal Square | 221. 最大正方形(优化的暴力解法+动态规划解法)

    题目 https://leetcode.com/problems/maximal-square/ 题解 方法1:最暴力解 O((m*n)^2) public class Solution {publi ...

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

  3. [leetcode ]221. Maximal Square c语言

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

  4. LeetCode Top 100 Liked Questions 221. Maximal Square (Java版; Medium)

    welcome to my blog LeetCode Top 100 Liked Questions 221. Maximal Square (Java版; Medium) 题目描述 Given a ...

  5. LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法

    题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...

  6. leetcode 85. Maximal Rectangle | 85. 最大矩形(单调栈)

    题目 https://leetcode.com/problems/maximal-rectangle/ 题解 本题与 leetcode 84. Largest Rectangle in Histogr ...

  7. 【动态规划】leetcode - Maximal Square

    称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  8. leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题

    Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...

  9. 经典算法题之Maximal Square

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 作者:叶    虎 Maximal Square是道非常有意思的算 ...

  10. 【DP】LeetCode 85. Maximal Rectangle

    LeetCode 85. Maximal Rectangle Solution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk ...

最新文章

  1. C语言register关键字—最快的关键字
  2. mac80211解析之发送速率控制
  3. (chap6 Http首部) 通用首部字段 Cache-Control
  4. 计组之中央处理器:1、CPU的功能和基本结构
  5. 数据产品-数据可视化大作“数据大屏”
  6. bash:附近有语法错误_Bash备忘单:按键组合和特殊语法
  7. 《大话设计模式》第29章-OOTV杯超级模式大赛—模式总结(五)
  8. 【图像去噪】基于matlab GUI均值+中值+高斯低通+多种小波变换图像去噪【含Matlab源码 856期】
  9. java教师考勤系统,javaweb课堂考勤管理系统
  10. 短信验证码和邮箱验证码
  11. python ocr文字识别竖排繁体_古籍族谱繁体竖排中文识别图文攻略-千百OCR
  12. 计算机无法访问家庭组内打印机,Win7电脑无法连接共享打印机拒绝访问怎么办...
  13. 怎样完成一次比较漂亮的晋升面试演讲
  14. 以前的windows安装文件可以删除吗_Windows系统中“C盘”可以删除的文件—让你最大限度提C盘空间...
  15. 第2关:比较、掩码和布尔逻辑
  16. 【《视觉SLAM十四讲》前ch2-ch6实践全过程和遇到的问题及解决办法】
  17. 基于PaddleNLP的中文对话文本匹配
  18. autoware官方入门教使用
  19. 刷(shui)题记录 2021.12
  20. html/css做一个简单的个人简历

热门文章

  1. memcached在项目中的应用
  2. 【转】NSBundle的使用,注意mainBundle和Custom Bundle的区别
  3. Java基础知识强化之集合框架笔记15:List集合的特点
  4. 业界资讯: Flash Player Incubator 改进
  5. linux中文件带方块,JFreeChart图片里的中文在linux下显示为方块的解决办法
  6. Redis 对象系统
  7. mongo的‘模糊匹配’
  8. thinkphp学习简易教程(一) thinkphp创建项目
  9. CC_STACKPROTECTOR防内核堆栈溢出补丁分析【转】
  10. cocos2d-x之物理世界(创建物理世界)