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. 1 0 1 0 0
  2. 1 0 1 1 1
  3. 1 1 1 1 1
  4. 1 0 0 1 0

Return 4.

  1. public class Solution {
  2. public int maximalSquare(char[][] matrix) {
  3. int rows = matrix.length;
  4. if(rows == 0) return 0;
  5. int cols = matrix[0].length;
  6. if(cols == 0) return 0;
  7. Node[][] sta = new Node[rows][cols];
  8. for(int i=0; i<rows; i++){
  9. for(int j=0; j<cols; j++){
  10. sta[i][j] = new Node();
  11. }
  12. }
  13.  
  14. int res = 0;
  15.  
  16. if(matrix[0][0] == '1'){
  17. sta[0][0].left = 1;
  18. sta[0][0].up = 1;
  19. sta[0][0].maxSize = 1;
  20. res = 1;
  21. }
  22. //求第一行
  23. for(int i=1; i<cols; i++){
  24. if(matrix[0][i] == '1'){
  25. sta[0][i].left = sta[0][i-1].left+1;
  26. sta[0][i].maxSize = 1;
  27. res = 1;
  28. }
  29. }
  30. //求第一列
  31. for(int j=1;j<rows;j++){
  32. if(matrix[j][0] == '1'){
  33. sta[j][0].up = sta[j-1][0].up + 1;
  34. sta[j][0].maxSize = 1;
  35. res = 1;
  36. }
  37. }
  38. //动态求其他
  39. for(int i=1; i<rows; i++){
  40. for(int j=1; j<cols; j++){
  41. if(matrix[i][j] == '1'){
  42. sta[i][j].left = sta[i-1][j].left + 1;
  43. sta[i][j].up = sta[i][j-1].up + 1;
  44. sta[i][j].maxSize = 1;
  45. if(matrix[i-1][j-1] == '1'){
  46. sta[i][j].maxSize = Math.min(sta[i][j].left, sta[i][j].up);
  47. sta[i][j].maxSize = Math.min(sta[i][j].maxSize, sta[i-1][j-1].maxSize+1);
  48. }
  49.  
  50. }
  51. res = Math.max(sta[i][j].maxSize, res);
  52. }
  53. }
  54.  
  55. return res*res;
  56.  
  57. }
  58.  
  59. class Node{
  60. int left;
  61. int up;
  62. int maxSize;
  63. }
  64.  
  65. }

动态规划例子: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. mysql 问题排查语句

    1.查询不是sleep或者有状态的sql select * from `information_schema`.processlist where command !='Sleep' or state ...

  2. 解决树莓派新版系统 ssh连接不了问题

    一.解决办法 由于有连接了显示器,所以可以直接输入命令行开启树莓派的SSH,并且使用putty连接就可以. sudo service ssh start sudo service ssh sttus ...

  3. 转:mac环境下使用svn

    在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...

  4. php中array_merge函数

    php中array_merge函数 一.array_merge简介 (PHP 4, PHP 5, PHP 7) array_merge — 合并一个或多个数组 说明¶ array array_merg ...

  5. SAR(遥感、卫星) 图像常用数据集

    Brazilian Coffee Scenes数据集较小,5MB左右: UC Merced Land Use Dataset(数据集规模较大,300MB+) MSTAR public targets ...

  6. 利用日志文件恢复MYSQL数据库

    利用日志文件恢复MYSQL数据库 650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic ...

  7. C#开发 —— 泛型,文件

    泛型的目标是采用广泛适用和可交互性的形式来表示算法和数据结构 —— 参数化 泛型能子啊编译时提供强大的类型检查,减少数据类型之间的显式转换,装箱操作和运行时的类型检查 泛型的类型参数T可以被看作是一个 ...

  8. 体验域名注册解析与SSL证书

  9. C语言-常量指针与指针常量

    最近倪健问我一个问题,他说:什么是常指针?什么是指向常变量的指针?请举例说明 我查阅资料后这么回答他了, 指针常量(常指针):int * const p : 指针是一个常量,也就是说它始终指向那个地址 ...

  10. offSet和client和scroll

    这三个是是js盒模型属性 client clientWidth 内容宽度加上左右padding clientHeight 内容高度加上上下padding clientTop 上边框的宽度 client ...