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. android 文本框不获取焦点的两种方式

    当进入一个页面以后,我们不希望EditText获取焦点自动弹出软键盘,占据大半个屏幕. 方法一 让LinearLayout率先获取焦点,代码如下: <LinearLayout android:f ...

  2. python简说(五)操作文件

    f = open('users.txt',encoding='utf-8') #读文件的时候,必须存在在才可以读 文件对象,或者文件句柄res = f.read()print(res)f.close( ...

  3. 2018年11月10日 input,print,pass 用法,条件语句+字符串

    name=input('请输入用户名')#永远等待用户输入 password=input('请输入密码') print(name) print(password) 变量定义的规则: 变量名只能是 字母 ...

  4. 如何使用jqueryUi的datepicker日历控件?

    参考: http://www.jb51.net/article/85007.htm 这里的日历控件是, 基于jquery的jqureyui中的一个 widget. 需要js 文件: 外部的js文件, ...

  5. windows进程中的几个杂项-hpguard 进程终止

    打印机后台进程 c:\windows\system32\spool printers,servers, tool等, 清空printers中的内容就可以正常打印; 锐爽的后视镜跟其他的车型的后视镜不一 ...

  6. LabVIEW编程实例:如何通过TCP协议进行数据通信

    对于网络通信来说,LabVIEW平台本身提供了多种方法加以实现,如可以通过TCP协议.UDP协议.DataSocket技术.甚至远程面板通信技术等方式进行通信. 下面通过一个简单的例子,演示在LabV ...

  7. hihoCoder week7 完全背包

    完全背包 题目链接 https://hihocoder.com/contest/hiho7/problem/1 #include <bits/stdc++.h> using namespa ...

  8. kubernetes 实战4_命令_Configure Pods and Containers

    Configure Service Accounts for Pods A service account provides an identity for processes that run in ...

  9. Latex 算法过长 分页显示方法

    参考: Algorithm tag and page break Latex 算法过长 分页显示方法 1.引用algorithm包: 2.在\begin{document}前加上以下Latex代码: ...

  10. 网页中动态嵌入PDF文件/在线预览PDF内容https://www.cnblogs.com/xgyy/p/6119459.html

    #网页中动态嵌入PDF文件/在线预览PDF内容# 摘要:在web开发时我们有时会需要在线预览PDF内容,在线嵌入pdf文件: 问题1:如何网页中嵌入PDF: 在网页中: 常用的几种PDF预览代码片段如 ...