221. 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.
链接: http://leetcode.com/problems/maximal-square/
题解:
二维DP,先初始化首行和首列,然后假设matrix[i][j] == '1',我们可以先设定dp[i][j] = 1,然后根据左上,上,左三个元素中最小的一个来求新的值。代码写得较繁琐,还可以优化空间复杂度。
Time Complexity - O(n2), Space Complexity - O(n2)
public class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix == null || matrix.length == 0)
return 0;
int[][] dp = new int[matrix.length][matrix[0].length];
int max = 0; for(int i = 0; i < matrix.length; i++) {
if(matrix[i][0] == '1') {
dp[i][0] = 1;
if(max == 0)
max = 1;
}
} for(int j = 0; j < matrix[0].length; j++) {
if(matrix[0][j] == '1') {
dp[0][j] = 1;
if(max == 0)
max = 1;
}
} for(int i = 1; i < dp.length; i++) {
for(int j = 1; j < dp[0].length; j++) {
if(matrix[i][j] == '1') {
dp[i][j] = 1;
if(dp[i - 1][j - 1] > 0
&& dp[i - 1][j] > 0
&& dp[i][j - 1] > 0) {
int prev = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1]));
prev = (int)Math.sqrt(prev) + 1;
prev *= prev;
dp[i][j] = prev;
max = Math.max(max, prev);
}
}
}
} return max;
}
}
二刷:
使用了与一刷相同的方法, 二维dp。先初始化dp矩阵行和列,再根据当前点上边,左边,左上边的三个点来决定是否是一个全一square,假如是一个全一square,那么我们还要根据这三个点里的最小值来求出当前点的值,要先开方,加1再平方。代码繁琐,速度也比较慢,说明功力还不足。这里dp[i][j]是全一正方形的元素数,这个设置并不好。
Java:
2D - DP
Time Complexity - O(mn), Space Complexity - O(mn)
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0) {
return 0;
}
int rowNum = matrix.length;
int colNum = matrix[0].length;
int[][] dp = new int[rowNum][colNum]; int max = 0;
for (int i = 0; i < rowNum; i++) {
if (matrix[i][0] == '1') {
dp[i][0] = 1;
max = 1;
}
}
for (int j = 0; j < colNum; j++) {
if (matrix[0][j] == '1') {
dp[0][j] = 1;
max = 1;
}
} for (int i = 1; i < rowNum; i++) {
for (int j = 1; j < colNum.length; j++) {
if (matrix[i][j] == '1') {
dp[i][j] = 1;
if (dp[i - 1][j - 1] > 0 && dp[i - 1][j] > 0 && dp[i][j - 1] > 0) {
int prev = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1]));
prev = (int)Math.sqrt(prev) + 1;
dp[i][j] = prev * prev;
}
max = Math.max(dp[i][j], max);
}
}
}
return max;
}
}
二维dp改进,我们改变dp[i][j]的设置,下面dp[i][j]代表以(i, j)为右下角的全一矩阵的最长边长,这样我们就可以避免每次计算sqrt以及作乘法, 只要最后返回maxLen * maxLen就可以了。速度从18%上升到了58%。
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0) {
return 0;
}
int rowNum = matrix.length;
int colNum = matrix[0].length;
int[][] dp = new int[rowNum][colNum]; int maxLen = 0;
for (int i = 0; i < rowNum; i++) {
if (matrix[i][0] == '1') {
dp[i][0] = 1;
maxLen = 1;
}
}
for (int j = 0; j < colNum; j++) {
if (matrix[0][j] == '1') {
dp[0][j] = 1;
maxLen = 1;
}
} for (int i = 1; i < rowNum; i++) {
for (int j = 1; j < colNum; j++) {
if (matrix[i][j] == '1') {
dp[i][j] = 1;
if (dp[i - 1][j - 1] > 0 && dp[i - 1][j] > 0 && dp[i][j - 1] > 0) {
dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
}
maxLen = Math.max(dp[i][j], maxLen);
}
}
}
return maxLen * maxLen;
}
}
一维DP:
这里因为dp[i][j]的值只和dp[i - 1][j - 1], dp[i - 1][j]以及dp[i][j - 1]相关,所以我们可以使用滚动数组来进行空间复杂度的优化,一行一行进行计算。思路大都借鉴了jianchao.li.figher大神的。我们先遍历首行和首列查找元素'1',假如有'1'则max可以设置为1。接下来使用了一个临时变量topLeft来保存topLeft元素,在matrix[i][j] == '1'的情况下,在滚动数组里我们仍然考虑左边元素 - dp[j - 1], 上方元素dp[j]以及左上元素topLeft。 我们预先保存dp[j]作为下一次计算的topLeft。 最后返回 maxLen * maxLen。
还要继续精炼代码。看过dietpepsi乐神的代码以后发现真是简短而且优美。
Time Complexity - O(mn), Space Complexity - O(n)
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0) {
return 0;
}
int rowNum = matrix.length;
int colNum = matrix[0].length;
int[] dp = new int[colNum]; int maxLen = 0; for (int j = 0; j < colNum; j++) {
if (matrix[0][j] == '1') {
dp[j] = 1;
maxLen = 1;
}
}
if (maxLen == 0) {
for (int i = 0; i < rowNum; i++) {
if (matrix[i][0] == '1') {
maxLen = 1;
break;
}
}
} int topLeft = 0;
for (int i = 1; i < rowNum; i++) {
for (int j = 1; j < colNum; j++) {
if (j == 1) {
topLeft = matrix[i - 1][j - 1] - '0';
dp[j - 1] = matrix[i][j - 1] - '0';
}
int tmp = dp[j];
if (matrix[i][j] == '1') {
if (dp[j] > 0 && dp[j - 1] > 0 && topLeft > 0) {
dp[j] = Math.min(dp[j - 1], Math.min(dp[j], topLeft)) + 1;
} else {
dp[j] = 1;
}
maxLen = Math.max(dp[j], maxLen);
} else {
dp[j] = 0;
}
topLeft = tmp;
}
}
return maxLen * maxLen;
}
}
一维DP的优化:
- 扩大初始化dp数组的size到colNum + 1,这样我们就不需要对首行和首列进行额外地判断。只需要在每次j = 1的时候设置dp[j - 1] = 0,以及topLeft = 0就可以了。代码还是不太好看,还可以继续优化代码。
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0) {
return 0;
}
int rowNum = matrix.length;
int colNum = matrix[0].length;
int[] dp = new int[colNum + 1];
int maxLen = 0;
int topLeft = 0; for (int i = 1; i <= rowNum; i++) {
for (int j = 1; j <= colNum; j++) {
if (j == 1) {
topLeft = 0;
dp[j - 1] = 0;
}
int tmp = dp[j];
if (matrix[i - 1][j - 1] == '1') {
if (dp[j] > 0 && dp[j - 1] > 0 && topLeft > 0) {
dp[j] = Math.min(dp[j - 1], Math.min(dp[j], topLeft)) + 1;
} else {
dp[j] = 1;
}
maxLen = Math.max(maxLen, dp[j]);
} else {
dp[j] = 0;
}
topLeft = tmp;
}
}
return maxLen * maxLen;
}
}
一维DP再优化:
下面又作了一些优化:
- 去除了 j == 1的判断,因为上面扩大了一维dp数组的size,所以dp[0]总是等于0
- 把topLeft的定义放在了外循环, 我们在处理每行之前,设置int topLeft = 0
- 简化了当matrix[i - 1][j - 1] == '1'时的逻辑。我们不需要判断左上,上和左三个点是否大于0, 只需要取三个点的min 再加1就可以了
到这里已经比较接近discuss里jianchao.li.fighter的版本了。 我们还可以把i 从 0 ~ rowNum进行遍历,这样就少作一个 i - 1的计算。那就基本和jianchao.li.fighter的解一样了。
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0) {
return 0;
}
int rowNum = matrix.length, colNum = matrix[0].length;
int[] dp = new int[colNum + 1];
int maxLen = 0; for (int i = 1; i <= rowNum; i++) {
int topLeft = 0;
for (int j = 1; j <= colNum; j++) {
int tmp = dp[j];
if (matrix[i - 1][j - 1] == '1') {
dp[j] = Math.min(dp[j - 1], Math.min(dp[j], topLeft)) + 1;
maxLen = Math.max(maxLen, dp[j]);
} else {
dp[j] = 0;
}
topLeft = tmp;
}
}
return maxLen * maxLen;
}
}
二维DP再优化
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0) {
return 0;
}
int rowNum = matrix.length, colNum = matrix[0].length;
int[][] dp = new int[rowNum + 1][colNum + 1];
int maxLen = 0; for (int i = 1; i <= rowNum; i++) {
for (int j = 1; j <= colNum; j++) {
if (matrix[i - 1][j - 1] == '1') {
dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1;
maxLen = Math.max(maxLen, dp[i][j]);
}
}
}
return maxLen * maxLen;
}
}
三刷:
Java:
二维dp
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0) return 0;
int rowNum = matrix.length, colNum = matrix[0].length;
int[][] dp = new int[rowNum + 1][colNum + 1];
int minLen = 0; for (int i = 1; i <= rowNum; i++) {
for (int j = 1; j <= colNum; j++) {
if (matrix[i - 1][j - 1] == '1') {
dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1]));
minLen = Math.max(minLen, dp[i][j]);
}
}
}
return minLen * minLen;
}
}
简化为一维dp:
主要还是使用了topLeft来代表左上角的值。要注意先用tmp保存当前dp[j],结束完这个位置的计算时更新topLeft。在每一行开始前充值topLeft = 0。还有就是matrix[i - 1][j - 1] = '0'的情况下我们要设置dp[j] = 0
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length == 0) return 0;
int rowNum = matrix.length, colNum = matrix[0].length;
int[] dp = new int[colNum + 1];
int minLen = 0; for (int i = 1; i <= rowNum; i++) {
int topLeft = 0;
for (int j = 1; j <= colNum; j++) {
int tmp = dp[j];
if (matrix[i - 1][j - 1] == '1') {
dp[j] = 1 + Math.min(topLeft, Math.min(dp[j], dp[j - 1]));
minLen = Math.max(minLen, dp[j]);
} else {
dp[j] = 0;
}
topLeft = tmp;
}
}
return minLen * minLen;
}
}
Reference:
https://leetcode.com/discuss/63211/java-8ms-python-112-ms-dp-solution-o-mn-time-one-pass
http://www.cnblogs.com/jcliBlogger/p/4548751.html
https://leetcode.com/discuss/38489/easy-solution-with-detailed-explanations-8ms-time-and-space
221. Maximal Square的更多相关文章
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [LeetCode] 221. Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- Java for LeetCode 221 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- 221. Maximal Square -- 矩阵中1组成的最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- (medium)LeetCode 221.Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- 221. Maximal Square(动态规划)
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
随机推荐
- Wininet笔记一
1, InternetOpen 创建根句柄,由下一层的 InternetOpenUrl 和 InternetConnect 使用,而 InternetConnect 创建的句柄又被之后的几个函数使用. ...
- JSON的使用
最近在项目中大量的使用了JSON, 发现JSON和XML的功能相近,都是一种数据传输格式.只是与XML相比JSON显得更加轻量级,使用也更加容易. JSON依赖的第三方jar包: commons-be ...
- VC6配置OpenCV1.0
懒得写字,故扫描:
- linux命令之vim使用-(转)vim的保存文件和退出命令
博客地址: http://blog.sina.com.cn/s/blog_5e357d2d0100zmth.html
- C#代码分层的好处
1.对于复杂的系统,分层让代码结构清晰,便于开发人员对系统进行整体的理解.把握.如果代码没有分层,把逻辑都写在一个方法里面的代码就好比是一本没有目录的文档,要找出其中某一节都要对全文遍览一次. 2.基 ...
- swfupload使用说明
网上的例子介绍的文档真的很多.下面简单介绍一下 SWFUpload的文件上传流程是这样的: 1.引入相应的js文件 2.实例化SWFUpload对象,传入一个配置参数对象进行各方面的配置. 3.点击S ...
- linux与windows共享剪贴板(clipboard)
linux与windows共享剪贴板(clipboard)的方法 先说两句废话,其实linux和windows之间不需要共享剪贴板,直接在putty中,按住SHIFT+鼠标选择就可以了. 但是作为一种 ...
- laravel方法汇总详解
1.whereRaw() 用原生的SQL语句来查询,whereRaw('select * from user') 就和 User::all()方法是一样的效果 2.whereBetween() 查询时 ...
- HDU 4548 美素数
Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“ ...
- P3381: [Usaco2004 Open]Cave Cows 2 洞穴里的牛之二
这题..思维上远没有上一题复杂,是一个裸的RMQ..利用倍增就可以解决了. var n,q,i,j,f,t,c:longint; a:array[..,..] of longint; function ...