标签:动态规划

题目描述:

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. java基础之Character类概述

    Character 类 在对象中包装一个基本类型 char 的值 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然 构造方法 public Cha ...

  2. HashMap 和 concurrentHashMap

    从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. 在JDK1.5中,伟大的Doug Lea给我们带来了concurrent包,从 ...

  3. Js获取或计算时间的相关操作

    //获取当前日期(年月日),如:2017-12-18 function getNowDate() { var dd = new Date(); var y = dd.getFullYear(); // ...

  4. js时间比较大小,时间加减

    第一种: //时间类比较 startTime= new Date(Date.parse(starttime)); endTime=new Date(Date.parse(endTime)); //进行 ...

  5. 跟我一起了解koa之在koa中使用redis

    第一步安装中间件 cnpm i koa-generic-session koa-redis 第二步引入中间件 在中间件中写入session 浏览器中会存储数据 第三步关于Redis来读取和存储数据 读 ...

  6. mysql出错的代码解析及解答

    1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据库失败 1010:不能删除数据目 ...

  7. Twisted的WEB开发

    1   简介 在WEB开发中,偶尔需要对HTTP协议更多底层细节进行控制,这时的django/web.py等等显然无法满足要求,所以只好求助于Twisted了.使用Twisted进行WEB开发,其实更 ...

  8. DataLakeAnalytics: 解析IP地址对应的国家城市地址的能力

    Data Lake Analytics 作为云上数据处理的枢纽,最近加入了通过IP地址查找对应的国家.省份.城市.ISP的函数, 今天带大家体验一下. 函数详细介绍 本次一共添加了下面这些函数: ip ...

  9. HZOI2019建造游乐园(play)组合数学,欧拉图

    题目:https://www.cnblogs.com/Juve/articles/11186805.html(密码是我的一个oj用户名) solution: 反正我是想不出来... 题目大意就是要求出 ...

  10. Autoware显示用户界面细节

    https://blog.csdn.net/jianxuezixuan/article/details/86015224