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存储. 更新方 ...
随机推荐
- Kafka排队:Apache Kafka作为消息传递系统
1.目标 在这个Apache Kafka教程中,我们将学习Apache Kafka Queuing 的概念 .基本上,Kafka中的排队是传统消息传递的模型之一.所以,让我们首先简要介绍Kafka ...
- 嵌入式02 STM32 实验04跑马灯
开学STM32 跑马灯的实验主要就是了解GPIO口的配置及使用,我这里是使用库函数进行编程,主要需要设置以下两方面: 1.使能需要使用的IO口的时钟,一共有A.B.C.D.E.F.G七组IO口 2.初 ...
- python之生成器yeild
python生成器Generator——yield 思考: 首先思考这样一个问题: 创建一个列表,但是内存受限,容量一定是有限的.那么如果创建了一个包含100万个元素的列表,不仅占用很大的存储空间,而 ...
- ssh远程连接一段时间会失效的问题
话不多讲,先说明我的环境和远程环境. 本地环境:Ubuntu18.04(client) 远程环境:Ubuntu16.04(server) 我的一个小项目部署在百度云的Ubuntu服务器上,需要经常使用 ...
- Springboot 之 静态资源路径配置
1.静态资源路径是指系统可以直接访问的路径,且路径下的所有文件均可被用户通过浏览器直接读取. 2.在Springboot中默认的静态资源路径有:classpath:/META-INF/resource ...
- .Net Core-类库中创建CodeFirst
本文仅用来学习记录. 搭建项目架构的时候,需要在类库中进行CodeFirst数据迁移 1.在项目的解决方案中,添加类库ERPFrame.Model 2.在类库项目中 添加实体模型和数据上下文 其中 ...
- 9 同时搜索多个index,或多个type
搜索所有index(慎用): GET /_search 搜一个索引下,所有type,(不指定type即可) GET /beauties/_search 搜多个索引,则多个索引间,用逗号(,)分隔开 ...
- dubbo源码阅读之自适应扩展
自适应扩展机制 刚开始看代码,其实并不能很好地理解dubbo的自适应扩展机制的作用,我们不妨先把代码的主要逻辑过一遍,梳理一下,在了解了代码细节之后,回过头再来思考自适应扩展的作用,dubbo为什么要 ...
- 试用一款网荐的 iOS 快速布局UI库
NerdyUI github: https://github.com/nerdycat/NerdyUI Cupcake (Swift 版本) github: https://github.com/ ...
- Vivado Non-Project Flow
# ----------------------------------------------------------------------------- # Vivado Non-Project ...