标签:动态规划

题目描述:

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. Linux查看温度

    step 1: centos $ sudo yum install lm_sensors ubuntu $ sudo apt-get install lm_sensors step2$ sudo se ...

  2. java RSA验签

    这几天在跟一个php的小哥哥联调接口,遇到了一些问题记录下来, 直接上代码吧,亲测有效 import org.slf4j.Logger; import org.slf4j.LoggerFactory; ...

  3. Python学习day24-面向对象的三大特征之继承

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. PKU--1267 Cash Machine(多重背包)

    题目http://poj.org/problem?id=1276 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...

  5. vue 路由跳转记住滚动位置,返回时回到上次滚动位置

    参考:https://blog.csdn.net/qq_40204835/article/details/79853685 方法一: 利用Keep-Alive和监听器 1.首先在路由中引入需要的模块 ...

  6. MapReduce 图解流程

    Anatomy of a MapReduce Job In MapReduce, a YARN application is called a Job. The implementation of t ...

  7. Spring 社区的唯一一个国产开源项目 - Spring Cloud Alibaba 毕业了

    阿里妹导读:一年多前,Java 界最近发生了一件大事,阿里开源 Spring Cloud Alibaba,并推出首个预览版.Spring Cloud 本身是一套微服务规范,并不是一个拿来即可用的框架, ...

  8. 尝试一下LLJ大佬的理论AC大法

    1.BZOJ 3522 Poi2014 Hotel DFS 给定一棵树,求有多少无序三元组(x,y,z)满足x,y,z互不相等且Dis(x,y)=Dis(y,z)=Dis(x,z) 枚举中心点,分别d ...

  9. Python技巧—list与字符串互相转换

    Python技巧-list与字符串互相转换 在Python的编程中,经常会涉及到字符串与list之间的转换问题,下面就将两者之间的转换做一个梳理. 1.list转换成字符串 命令:list() 例子: ...

  10. 常用 docker 容器 使用

    mongo: 单点 docker run -idt --name=mongo --restart=always -p : -v /home/hylas/opt/mongo/data:/data/db ...