原题链接在这里:https://leetcode.com/problems/maximal-square/

题目:

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 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.

题解:

DP, 状态: 以当前点为右下角的最大都包含1的square边长.

转移方程 如果当前点为1, dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])+1.

初始化dp[m+1][n+1], 多一行一列方便处理边界.

res 一直取maintain dp的最大值.

优化dp成一行, 因为只需要左, 上, 和左斜上三个值. prev 来保存左斜上.

Time Complexity: O(m*n). m = matrix.length, n = matrix[0].length.

Space: O(n).

AC Java:

 public class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return 0;
} int res = 0;
int m = matrix.length;
int n = matrix[0].length;
int [] dp = new int[n+1];
int prev = 0; for(int i = 1; i<=m; i++){
for(int j = 1; j<=n; j++){
int temp = dp[j];
if(matrix[i-1][j-1] == '1'){
dp[j] = Math.min(Math.min(dp[j], dp[j-1]), prev) + 1;
}else{
dp[j] = 0;
}
prev = temp;
res = Math.max(res, dp[j]);
}
}
return res*res;
}
}

跟上Maximal Rectangle.

LeetCode Maximal Square的更多相关文章

  1. 【动态规划】leetcode - Maximal Square

    称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  2. [LeetCode] Maximal Square 最大正方形

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

  3. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

  4. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  5. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  6. 【LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  7. 【刷题-LeetCode】221. Maximal Square

    Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...

  8. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  9. [LintCode] Maximal Square 最大正方形

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

随机推荐

  1. SecureCrt自动化

    Crt自动化 测试 SecureCrt脚本 JS脚本   引言 软件介绍 脚本介绍 引言 在嵌入式公司中,面对大量的网络设备,不论开发同事进行设备开发.测试同事进行大量测试工作还是运维人员进行大量设备 ...

  2. 原生js记住密码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 如何使用 Migration创建一个迁移

    切换到YII所在的目录 yii migrate/create test

  4. PHP下利用PHPMailer配合QQ邮箱下的域名邮箱发送邮件(转)

    首先确定不是开启socks openssl phpinfo就可以知道 下载phpmailer   地址:https://github.com/PHPMailer/PHPMailer 下载完整, 个人和 ...

  5. [ZZ] Deferred Rendering and HDR

    http://www.gamedev.net/topic/496785-deferred-rendering-and-hdr/ Quote: Original post by jstrohYeah I ...

  6. UITextField里面的 placeholder颜色和字体

    //  placeholder 颜色 [field setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor& ...

  7. Yii源码阅读笔记(二)

    接下来阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— namespace yii; use yii\base\InvalidConfigExceptio ...

  8. wflag

    http://stackoverflow.com/questions/41312622/how-to-echo-an-alert-in-php-a-string-some-confusion-with ...

  9. Flink - FLIP

    https://cwiki.apache.org/confluence/display/FLINK/Flink+Improvement+Proposals FLIP-1 : Fine Grained ...

  10. springMVC框架下——通用接口之图片上传接口

    我所想要的图片上传接口是指服务器端在完成图片上传后,返回一个可访问的图片地址. spring mvc框架下图片上传非常简单,如下 @RequestMapping(value="/upload ...