LintCode刷题笔记-- 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.这一题明显使用动态规划来解题,开始想法使用动态规划的方法来记录所有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的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackII
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
随机推荐
- cdh_hadoop下载地址
http://archive.cloudera.com/cdh5/cdh/5/
- 已发布的jsp项目如何在本地展示
已发布的项目可以放到tomcat 安装目录下的webapps/下面,但是有时候我们的目录已经放好了,所在要加映射过去. 1, 到tomcat/conf/下,打开server.xml 2, 找到 < ...
- leetcode242 Valid Anagram
lc242 Valid Anagram 直接统计每种字母出现次数即可 class Solution { public boolean isAnagram(String s, String t) { i ...
- HZOI2019建造游乐园(play)组合数学,欧拉图
题目:https://www.cnblogs.com/Juve/articles/11186805.html(密码是我的一个oj用户名) solution: 反正我是想不出来... 题目大意就是要求出 ...
- Django项目:CRM(客户关系管理系统)--74--64PerfectCRM实现CRM课程排名详情
#urls.py """PerfectCRM URL Configuration The `urlpatterns` list routes URLs to views. ...
- Tool-MySQL-SQLyog:SQLyog
ylbtech-Tool-MySQL-SQLyog:SQLyog SQLyog 是一个快速而简洁的图形化管理MYSQL数据库的工具,它能够在任何地点有效地管理你的数据库,由业界著名的Webyog公司出 ...
- 百钱白鸡(for循环的练习)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 13 个最佳 JavaScript 数据网格库
13 个最佳 JavaScript 数据网格库 转自:开源中国 www.oschina.net/translate/best-javascript-data-grid-libraries Java ...
- jmeter体系结构
jmeter体系结构 jmeter体系结构: 1.取样器.断言.监听器组合在一起就可以帮助我们完成发送请求.验证结果及记录结果三项工作 (1)取样器的访问路径:[测试计划]---[线程组] ...
- 2019-2-19-win10-uwp-客户端如何发送类到-asp-dotnet-core-作为参数
title author date CreateTime categories win10 uwp 客户端如何发送类到 asp dotnet core 作为参数 lindexi 2019-2-19 9 ...