Question

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

For example, given the following matrix:

1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Return 4.

Solution

2-D array. The changing condition is:

t[i][j] = min(t[i][j-1], t[i-1][j], t[i-1][j-1]) + 1.

 public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length < 1)
return 0;
int row = matrix.length, column = matrix[0].length, max = 0;
int[][] dp = new int[row][column];
// Top Row
for (int i = 0; i < column; i++) {
if (matrix[0][i] == '0')
dp[0][i] = 0;
else
dp[0][i] = 1;
}
// Left Column
for (int i = 0; i < row; i++) {
if (matrix[i][0] == '0')
dp[i][0] = 0;
else
dp[i][0] = 1;
}
// Inside Filling
for (int i = 1; i < row; i++) {
for (int j = 1; j < column; j++) {
if (matrix[i][j] == '0') {
dp[i][j] = 0;
} else {
int tmp = Math.min(dp[i][j - 1], dp[i - 1][j]);
dp[i][j] = Math.min(tmp, dp[i - 1][j - 1]) + 1;
}
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++)
max = Math.max(max, dp[i][j]);
}
return max * max;
}
}

Maximal Square 解答的更多相关文章

  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. 【POJ2739】Sum of Consecutive Prime Numbers

    简单的素数打表,然后枚举.开始没注意n读到0结束,TLE了回..下次再认真点.A过后讨论里面有个暴力打表过的,给跪了! #include <iostream> #include <c ...

  2. python练习linux下创建路径

    #coding=utf-8 import os class MakeDirectory(): def mkdir(self,path): # 去除首位空格 path=path.strip() # 去除 ...

  3. 神坑 关于&&的取值

    a = 0&&"ssss": 结果a=0 a=true&&"w": 结果a=w: 类似于 前面是真的 会执行后面并返回后面 前面 ...

  4. eq,neq,gt,lt等表达式缩写

    eq 等于neq 不等于gt 大于egt 大于等于lt 小于elt 小于等于like LIKEbetween BETWEENnotnull IS NUT NULLnull IS NULL

  5. 话说GET与POST那点恩怨

    看过很多人写GET和POST之间的区别,为什么这么多人关注它们呢?因为它们是最常用的两种HTTP方法,之间有很多相同之处,也存在非常大的不同.首先了解一下HTTP方法: 什么是HTTP?     超文 ...

  6. JS学习笔记-数组

    ECMAScript中没有提供类和接口等的定义,但它却是一门面向对象的语言,由于它能够通过其它 方式实现类似高级语言的面向对象功能,这些内容将在后面的文章中进行一步步的总结.此篇仅对JS中对象作简要说 ...

  7. Impala与Hive的比較

    1. Impala架构        Impala是Cloudera在受到Google的Dremel启示下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的Hive+MapReduce批 ...

  8. [Hapi.js] Request Validation with Joi

    hapi supports request validation out of the box using the joi module. Request path parameters, paylo ...

  9. 利用boost获取时间并格式化

    利用boost来获取当前时间又方便快捷,还不用考虑跨平台的问题. 1. 输出YYYYMMDD #include <boost/date_time/gregorian/gregorian.hpp& ...

  10. Js 实现 C# Format方法

    参考网友的, 挺好用的: String.prototype.format = function (args) { if (arguments.length > 0) { var result = ...