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. 线程框架Executor的用法举例

    java5线程框架Executor的用法举例 Executor 是 java5 下的一个多任务并发执行框架(Doug Lea),可以建立一个类似数据库连接池的线程池来执行任务.这个框架主要由三个接口和 ...

  2. 不允许 ASSIGNMENT 语句中包含 FOR XML 子句。

    DECLARE @guidList NVARCHAR(max) SELECT @guidList=( CAST(OrderGUID AS nvarchar(max)) +',') FROM Order ...

  3. UVA Building designing

    题目总结来说求一段序列,必须正负交替,且绝对值递增 #include <iostream> #include <cstdio> #include <cstring> ...

  4. C# Dapper 基本使用 增删改查事务等

    using DapperTest.Models; using System.Collections.Generic; using System.Web.Http; using Dapper; usin ...

  5. 转:IOS的推送。是一个强大的功能

    IOS下我们很多人天天开着 邮件推送 微信推送 QQ推送 微博推送 新浪微博推送,安卓敢吗? 五个后台进程消耗电.流量谁敢这么做?现在安卓也出了推送,但绝对是伪推送.实际是挂了个进程,关掉了就收不到. ...

  6. HDOJ 5419 Victor and Toys 树状数组

    分母是一定的C(m,3) 树状数组求每一个数能够在那些段中出现,若x出如今了s段中,分子加上w[x]*C(s,3) Victor and Toys Time Limit: 2000/1000 MS ( ...

  7. tomcat+nginx+redis实现均衡负载以及session共享

    1.redis简介及下载安装 作为这次的主角,相信大家对redis应该都一定印象,redis是一款开源的高性能key-value数据库,拥有丰富的键值储存类型,并提供多种语言的API. 与一般数据库不 ...

  8. POJ 2430 状压DP

    题意: 思路: 先预处理出所有格子的statement statement=1–>只有上边的格子被覆盖 statement=2–>只有下边的格子被覆盖 statement=3–>上下 ...

  9. OpenCV —— 摄像机模型与标定

    这种理论看的已经够多了,感觉应用价值不大(矫正畸变图像还凑合,用摄像机测距神马的...) 有始有终吧,简单把内容梳理一下 针孔  摄像机模型 —— 过于理想(不能为快速曝光收集足够的光线) 透镜可以聚 ...

  10. 记录一个mybatis编写xml遇到的错误:java.lang.unsupportedOperationException

    写完xml里的sql在执行xml中的sql时报错,经过排查找到问题出在方法中的resultType这个属性的类型上 如图所示:只需要将sortedSet改为set集合里所存储的对象的类型即可. 这里我 ...