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的更多相关文章

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

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

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

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

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

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

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

  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 Square 最大正方形

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

  9. LeetCode Maximal Square

    原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...

随机推荐

  1. SpringBoot配置文件敏感信息加密-jasypt

    使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些.打开application.properties或application.yml,比如mysq ...

  2. vs中web api程序不包含适合于入口点的静态“Main”方法

    步骤:选择该项目的属性--应用程序--输出类型--类库

  3. Python-16-继承、封装、多态

    一.继承 1. 概念 继承是一种创建新类的方式,新建的类可以继承一个或多个父类(python支持多继承),父类又可称为基类或超类,新建的类称为派生类或子类. 子类会“”遗传”父类的属性,从而解决代码重 ...

  4. LeetCode977.Squares of a Sorted Array

    题目 977. Squares of a Sorted Array Given an array of integers A sorted in non-decreasing order, retur ...

  5. windows10环境下的RabbitMQ使用_笔记

    使用默认账号:guest/guest登录http://localhost:15672/#/进去,添加一个新用户(Administrator权限),并设置其Permission 新建两个控制台程序 安装 ...

  6. yum安装k8s集群

    k8s的安装有多种方式,如yum安装,kubeadm安装,二进制安装等.本文是入门系列,只是为了快速了解k8s的原理和工作过程,对k8s有一个快速的了解,这里直接采用yum安装 的1.5.2为案例进行 ...

  7. Form' threw an exception of type 'System.InvalidOperationException'

    环境:VS2017 NetCore 2.2 Razor Layui 在处理异步请求是遇到"((Microsoft.AspNetCore.Http.Internal.DefaultHttpRe ...

  8. 结合consul raft库理解raft

    一 入口 github.com/hashicorp/consul/agent/consul/server.go func (s *Server) setupRaft() error { 状态机,用于c ...

  9. JavaTCP粘包、拆包

    import java.nio.ByteBuffer; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBu ...

  10. Oracle——无法在查询中执行 DML 操作

    今天在调用Oracle Function遇到一个异常