Maximal Square II
Description
Given a 2D binary matrix filled with 0's and 1's, find the largest square which diagonal is all 1 and others is 0.
Only consider the main diagonal situation.
Example
Example 1:
Input:
[[1,0,1,0,0],[1,0,0,1,0],[1,1,0,0,1],[1,0,0,1,0]]
Output:
9
Explanation:
[0,2]->[2,4]
Example 2:
Input:
[[1,0,1,0,1],[1,0,0,1,1],[1,1,1,1,1],[1,0,0,1,0]]
Output:
4
Explanation:
[0,2]->[1,3]
思路:动态规划,u和l数组分别代表左边三角形的最大值和上方三角形的最大值,而f代表对角线到此点的最大长度。
直接三者求最小值转移即可。
public int maxSquare2(int[][] matrix) {
// write your code here
int n = matrix.length;
if (n == 0)
return 0;
int m = matrix[0].length;
if (m == 0)
return 0;
int[][] f = new int[n][m];
int[][] u = new int[n][m];
int[][] l = new int[n][m];
int length = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
if (matrix[i][j] == 0) {
f[i][j] = 0;
u[i][j] = l[i][j] = 1;
if (i > 0)
u[i][j] = u[i - 1][j] + 1;
if (j > 0)
l[i][j] = l[i][j - 1] + 1;
} else {
u[i][j] = l[i][j] = 0;
if (i > 0 && j > 0)
f[i][j] = Math.min(f[i - 1][j - 1], Math.min(u[i - 1][j], l[i][j - 1])) + 1;
else
f[i][j] = 1;
}
length = Math.max(length, f[i][j]);
}
return length * length;
}
}
Maximal Square II的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 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 ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- 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 ...
- 【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] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- LeetCode Maximal Square
原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...
随机推荐
- 3.JVM 垃圾收集器
Garbage Collect(垃圾回收) 1.1 如何确定一个对象是垃圾? 要想进行垃圾回收,得先知道什么样的对象是垃圾. 1.1.1 引用计数法 对于某个对象而言,只要应用程序中持有该对象的引用, ...
- Weave跨主机实现docker互通,固定ip及dns使用介绍
一.weave介绍Weave是由weaveworks公司开发的解决Docker跨主机网络的解决方案,现在就采用它来实现Docker多宿主机互联的目的,它能够创建一个虚拟网络,用于连接部署在多台主机上的 ...
- centos7安装php7.3
安装php7.3 CentOS/RHEL 7.x: yum install epel-release yum install http://rpms.remirepo.net/enterprise/r ...
- Fiddler代理手机抓包
Fiddler代理手机抓包 0. 安装Fiddler 1. 安装HTTPS证书并允许远程连接 Tools - Options 点击OK后重启Fiddler. 确保手机与当前电脑在同一局域网. 然后在手 ...
- docker容器与主机之间的文件复制
Easy—— 注意:“容器名与文件路径中间的冒号后不能有空格!” 1.从容器复制到主机: sudo docker cp 容器名:文件路径 主机文件/夹路径 2.从主机复制复制到容器: sudo doc ...
- VBA 打印及破密
Sub 打印()ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=TrueCall dyEnd SubSub dy()Dim a%, b ...
- Base64图片编码的使用
一.base64编码介绍 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,Base64编码可用于在HTTP环境下传递较长的标识信息.采用Base64编码具有不可读性,即所编码的数据 ...
- Java序列化流
1.什么是序列化流 序列化就是把Java对象“流化”,序列化后的Java对象可以保存到本地文件系统,或者通过Socket传输到其他的服务器. Java中对象序列化有以下特点: 1)类实现java.io ...
- 深入剖析Linux IO原理和几种零拷贝机制的实现
深入剖析Linux IO原理和几种零拷贝机制的实现 来源 https://zhuanlan.zhihu.com/p/83398714 零壹技术栈 公众号[零壹技术栈] 前言 零拷贝(Zero ...
- Qt 窗口相关的常用操作
PS: 本文使用的是Qt 4.8.4版本,不同版本代码可能会有差异 设置窗口标题 setWindowTitle(QString::fromLocal8Bit("易语言")); 禁用 ...