标签:动态规划

题目描述:

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.

Example

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.这一题明显使用动态规划来解题,开始想法使用动态规划的方法来记录所有1连在一起的面积,之后记录该面积右下角的坐标,之后通过遍历一个方向的坐标来得到一条边长,之后使用面积来相除,得到一条短边的长度,即可求出正方形面积。但是这么做存在一个问题,即,面积所得出的结果是两边相乘的结果,如果体现在一个数值上,是无法应用到下一子状态的:比如,2*6=12 先前子状态最大面积为12,然而在下一子状态中是无法确定12是从何而来,可能是3*4,可能是2*6。无法使用一个数值来记录两个变量。

2. 随即求助于其他解法,如果一个点是一个正方形的右下角,那么它的左上,上方和左方一定存在着至少存在一个正方形,最优情况是三个正方形的边长是相同的,这样直接在边长上加上1就会构成一个新的正方形,然而如果三个方向的正方形不等的话如果选择最大的边长,就会在某个方向上缺失一角。所以正确的方式是找出最小的一个正方形之后加上1,构成新的正方形。

3.如果matrix的位置上为0,则之前所积累的正方形边长将会中断,所有在为0的dp[i][j]上的位置为0

参考代码:

 public int maxSquare(int[][] matrix) {
// write your code here
int row = matrix.length;
int colum = matrix[0].length;
int[][] dp = new int[row][colum];
int max = 0;
dp[0][0] = matrix[0][0];
for(int i=1; i<row; i++){
dp[i][0] = matrix[i][0];
max = Math.max(dp[i][0], max); } for(int j=1; j<colum; j++){
dp[0][j] = matrix[0][j];
max = Math.max(dp[0][j],max); } for(int i=1; i<row; i++){
for(int j=1; j<colum; j++){
dp[i][j] = matrix[i][j]==1?Math.min(dp[i-1][j-1], Math.min(dp[i-1][j],dp[i][j-1]))+1:0;
max = Math.max(max, dp[i][j]);
}
}
return max*max;
}

LintCode刷题笔记-- Maximal Square的更多相关文章

  1. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  2. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  3. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  4. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  5. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  6. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  7. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

  8. LintCode刷题笔记-- BackpackII

    标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...

  9. LintCode刷题笔记-- Update Bits

    标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...

随机推荐

  1. 关于slf4j和log4j冲突问题(自己项目配置文件不生效)

    用-Dlog4j.debug可以打印出配置log4j的配置文件加载的信息 mvn dependency:tree 看依赖信息 然后排除掉重复得依赖 <dependencies> <d ...

  2. 牛客NOIP暑期七天营-TG1 赛后题解

    目录 牛客NOIP暑期七天营-提高组1 A-最短路 题目描述 link 题解 代码 B-最小生成链 题目描述 link 题解 代码 C-最小字典最短路 题目描述 link 题解 Update 牛客NO ...

  3. 2、mysql密码过期的修改方法(your password has expired)

    今天打开SQLyog提示密码过期:Your password has expired 解决方法:    1.  启动MySQL服务 2.  启动MySQL后台 3.  执行以下命令 step 1: S ...

  4. select 下拉框多选

    需要引入插件:fselect.js (此插件依赖jQ) 和 fselect.css  下载 点击查看在线演示地址 //html<select class="demo" mul ...

  5. Spring注解驱动开发(五)-----扩展原理

    扩展原理 1.BeanPostProcessor-----bean后置处理器,bean创建对象初始化前后进行拦截工作的 2.BeanFactoryPostProcessor-----beanFacto ...

  6. ROCR包中ROC曲线计算是取大于cutoff还是大于等于cutoff

    找到对应的代码如下 .compute.unnormalized.roc.curve function (predictions, labels) { pos.label <- levels(la ...

  7. Python字符串切片操作知识详解

    Python字符串切片操作知识详解 这篇文章主要介绍了Python中字符串切片操作 的相关资料,需要的朋友可以参考下 一:取字符串中第几个字符 print "Hello"[0] 表 ...

  8. 网易DMARC设置详解

    网易企业邮箱DMARC协议设置详解 发布时间:2013-09-27 尊敬的企业用户: 据统计,全球范围内被投递的钓鱼邮件每天约达到1亿封,无孔不入的钓鱼邮件.诈骗邮件,威胁着用户的信息和财产安全.网易 ...

  9. this的五种指法

    this 到底指向哪里 以下如果没提及,则为严格模式. js中作用域有两种: 词法作用域 动态作用域 词法作用域 词法作用域指在书写代码时就被确定的作用域.看如下代码 var value = 1; f ...

  10. 自定义事件javascript

    自定义事件 1.event构造函数自定义事件 /* * 自定义一个名为build的事件 * bubbles :事件是否冒泡 * cancelable:是否阻止事件的默认操作 * composed:指示 ...