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.

 public class Solution {
public int maximalSquare(char[][] matrix) {
int rows = matrix.length;
if(rows == 0) return 0;
int cols = matrix[0].length;
if(cols == 0) return 0;
Node[][] sta = new Node[rows][cols];
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
sta[i][j] = new Node();
}
} int res = 0; if(matrix[0][0] == '1'){
sta[0][0].left = 1;
sta[0][0].up = 1;
sta[0][0].maxSize = 1;
res = 1;
}
//求第一行
for(int i=1; i<cols; i++){
if(matrix[0][i] == '1'){
sta[0][i].left = sta[0][i-1].left+1;
sta[0][i].maxSize = 1;
res = 1;
}
}
//求第一列
for(int j=1;j<rows;j++){
if(matrix[j][0] == '1'){
sta[j][0].up = sta[j-1][0].up + 1;
sta[j][0].maxSize = 1;
res = 1;
}
}
//动态求其他
for(int i=1; i<rows; i++){
for(int j=1; j<cols; j++){
if(matrix[i][j] == '1'){
sta[i][j].left = sta[i-1][j].left + 1;
sta[i][j].up = sta[i][j-1].up + 1;
sta[i][j].maxSize = 1;
if(matrix[i-1][j-1] == '1'){
sta[i][j].maxSize = Math.min(sta[i][j].left, sta[i][j].up);
sta[i][j].maxSize = Math.min(sta[i][j].maxSize, sta[i-1][j-1].maxSize+1);
} }
res = Math.max(sta[i][j].maxSize, res);
}
} return res*res; } class Node{
int left;
int up;
int maxSize;
} }

动态规划例子:Maximal Square的更多相关文章

  1. 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 ...

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

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

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

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

  4. 【刷题-LeetCode】221. Maximal Square

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

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

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

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

  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. 221. Maximal Square(动态规划)

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

  9. 动态规划-最大的正方形面积 Maximal Square

    2018-09-13 19:19:44 问题描述: 问题求解: 方法一: 使用动态规划来求解,算法时间复杂度O(n^2). dp[i][j] : 以(i, j)为右下角的面积最大的正方形的边长. 初始 ...

随机推荐

  1. javaScript 预编译过程浅尝

    javaScript 预编译过程 1.创建AO对象(Activation Object) AO{ a: } 2.找形参和变量声明,将变量和形参作为AO属性名,值为undefined AO{ a:und ...

  2. pycharm 添加注册码

    https://blog.csdn.net/u014044812/article/details/78727496

  3. ZOJ Problem Set - 3822Domination(DP)

    ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...

  4. 文件/文件夹权限设置命令chmod的具体使用方法

    chmod是文件/文件夹权限设置的命令,在Linux中常常遇到.本博文下面总结chmod的具体使用方法. Linux/Unix的档案调用权限分为三级,即档案拥有者user.群组group.其它othe ...

  5. Codeforces 474D Flowers (线性dp 找规律)

    D. Flowers time limit per test:1.5 seconds memory limit per test:256 megabytes We saw the little gam ...

  6. OL记载Arcgis Server切片

    概述: 本文讲述怎样在OpenLayers中调用Arcgis Server切片并显示. 思路: 在OpenLayers中载入Arcgis Server切片用XYZ图层,Arcgis Server的切片 ...

  7. C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序

    C# 插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序 以下列出了数据结构与算法的八种基本排序:插入排序 冒泡排序 选择排序 高速排序 堆排序 归并排序 基数排序 希尔排序 ...

  8. 给Linux添加新用户,新建用户,新建帐号

    给Linux添加新用户,新建用户,新建帐号 添加用户组 sudo groupadd groupname 添加用户 sudo useradd username -m -s /sbin/nologin - ...

  9. Java图像渐变

    图像渐变我们大体想一下思路无非是这样:将图像所有的像素点的RBG,每个点就减去相同的量,而且这个量是个渐变的量.是的,就是这样,我们的程序也是这个思路,不过就是没有单纯的“想”这么简单了.我这里只编写 ...

  10. Looksery Cup 2015 Editorial

    下面是题解,做的不好.下一步的目标是rating涨到 1800,没打过几次cf A. Face Detection Author: Monyura One should iterate through ...