2018-09-13 19:19:44

问题描述:

问题求解:

方法一:

使用动态规划来求解,算法时间复杂度O(n^2)。

dp[i][j] : 以(i, j)为右下角的面积最大的正方形的边长。

初始条件:最上面一行,最左边一列,可以直接得到dp值。

更新公式:matrix[i][j] == '0' - > dp[i][j] = 0

        matrix[i][j] == '1' - > dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1], dp[i][j - 1]) + 1

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

动态规划-最大的正方形面积 Maximal Square的更多相关文章

  1. LeetCode 221. 最大正方形(Maximal Square)

    题目描述 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: ...

  2. [Leetcode221]最大面积 Maximal Square

    [题目] Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's a ...

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

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

  4. 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 ...

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

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

  6. [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 ...

  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. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  9. 【LeetCode】221. Maximal Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...

随机推荐

  1. log4j升级到logback

    虽然现在log4j已经基本上不更新很久了,但实际上升级log4j到logback最大的难度并不在于本身的替换,而是现有大量的三方jar依然使用log4j,以至于无法100%的exclude掉,所以很有 ...

  2. 【python40--类和对象:一些相关的BIF】

    0.如何判断一个类是否为另外一个类的子类 --使用issubclass(class,classinfo)函数,如果第一个函数(class)是第二个参数(classinfo)的一个子类,则返回Ture, ...

  3. 前端 --- 7 Bootstrop框架

    一.基本结构 1.下载地址   官方地址:https://getbootstrap.com 中文地址:http://www.bootcss.com/ 2.HTML基本搭建结构 <!DOCTYPE ...

  4. javascript对文件的读写

    整合了一下网上对于js实现文件读写的代码,但是该功能只能在ie浏览器下执行,另外有些电脑上的ie需要设置. 下面是写入代码: var fso = new ActiveXObject("Scr ...

  5. jsoup对 HTML 文档的解析和操作

    本文手动转载自http://www.cnblogs.com/chenying99/archive/2013/01/04/2844615.html,仅根据个人需要对实用部分进行转载,详细请阅读原文. j ...

  6. Python3基础 dict 创建字典 空字典

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. Python3基础 e记法示例

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. codevs1048石子归并

    codevs1048 石子归并  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 传送门  http://codevs.cn/problem/1048/ 题目描述 ...

  9. Math.random 随机数方法

    随机取数方法 Math.random() 表示0到1之间随机取一个数 <x< 小数 Math.random()* 表示0<x< parseInt(Math.random()*) ...

  10. 17秋 SDN课程 第五次上机作业

    17秋 SDN课程 第五次上机作业 Project:https://github.com/Wasdns/new_balance Slide is available at https://github ...