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. 选择排序(Selection Sort)

    选择排序就是在选择数组元素上做文章,关键是如何选择?选择的标准是什么?选择之后放在哪?所有这些都是选择排序的问题. 选择排序算法中,通常会有以下操作: 从数组第一个元素开始. 遍历整个数组,找到最小的 ...

  2. Ajax长连接

    所谓的长连接,就是不断去发送请求,把请求阻塞在服务器端,每次超过请求时间就去重新发送请求,保持连接,随时获取服务器端的响应的数据 项目案例: function connection(){       ...

  3. android jni (5)——Field & Method --> Accessing Mehtod

    在java编程语言中有非静态成员函数和静态成员函数,JNI允许我们访问到java中的成员函数,然后再jni中调用,这里我就来举例说明在jni中是如何做到的. 我们先在java中定义2个成员函数,一个非 ...

  4. Android——SQLite实现面向对象CRUD

    android中SQLite的使用,事实上倒也不难.可是与JDBC操作数据库相比,这个还是有点不顺手,并且我好久没写底层的封装了,使用SSM框架这些都不须要考虑......好了,废话不多说.以下直接建 ...

  5. C++中malloc/free和new/delete 的使用

    malloc/free 的使用要点 函数malloc的原型如下: void * malloc(size_t size); 用malloc申请一块长度为length的整数类型的内存,程序如下: int ...

  6. .net批量删除和添加

    往页面上拖一个GridView,设置好数据源,并为GridView添加一个模板列,往模板列里添加一个chekcbox,比如下面的代码 <asp:GridView ID="GridVie ...

  7. MySql命令——命令行客户机的分隔符

    delimiter // create procedure productpricint() begin select avg(price) as priceaverage from product; ...

  8. js判断ie浏览器

    function isIE() { //ie? if (!!window.ActiveXObject || "ActiveXObject" in window){ document ...

  9. Android ListView使用(非原创)

    1.ListView:它以列表的形式展示具体要显示的内容,并且能够根据数据的长度自适应屏幕显示 <LinearLayout xmlns:android="http://schemas. ...

  10. 自定义UIView动画效果

    最普通动画: //开始动画 [UIView beginAnimations:nil context:nil]; //设定动画持续时间 [UIView setAnimationDuration:]; / ...