方法一:

对于方阵中的每个点来说,先看纵向最多有多少个连续的1(例如:k个),然后从k到2开始枚举正方形的边长(设为j),每次判断能否形成一个正方形的时候去判断这j行是否每行都有连续j个1 。

具体做法:

1. 对于每个点统计以这个点为起点,横向有多少个1,纵向有多少个1,并得到分别得到两个矩阵。

2. 按照之前的普通思路进行判断

 1 import math
 2 def get_1_0_matrix(mat):
 3     new_mat_row = []
 4     new_mat_col = []
 5     new_mat_all_col = []
 6     rows = len(mat)
 7     cols = len(mat[0])
 8     #get the rows of 1
 9     for i in range(rows):
10         new_mat_row.append([])
11         new_mat_col.append([])
12         new_mat_all_col.append([])
13         cnt_step = 0
14         for j in range(cols):
15             if cnt_step>0:
16                 cnt_step -= 1
17             else:
18                 if mat[i][j]=='1':
19                     cnt_1 = 1
20                     for k in range(j+1,cols):
21                         if mat[i][k]=='1':
22                             cnt_1 += 1
23                             cnt_step += 1
24                         else:
25                             break
26                     for k in range(j,j+cnt_1):
27                         #new_mat_row[i][k] = chr(cnt_1)
28                         #new_mat_row[i] += str(cnt_1)
29                         new_mat_row[i].append(cnt_1)
30                         cnt_1 -= 1
31                 else:
32                     #new_mat_row[i] += "0"
33                     new_mat_row[i].append(0)
34     #get the cols of 1
35     for i in range(cols):
36         cnt_step = 0
37         for j in range(rows):
38             if cnt_step>0:
39                 cnt_step -= 1
40             else:
41                 if mat[j][i]=='1':
42                     cnt_1 = 1
43                     for k in range(j+1,rows):
44                         if mat[k][i]=='1':
45                             cnt_1 += 1
46                             cnt_step += 1
47                         else:
48                             break
49                     #print "cnt1:",cnt_1
50                     cnt_1_bk = cnt_1
51                     for k in range(j,j+cnt_1):
52                         #new_mat_col[k] += str(cnt_1)
53                         new_mat_col[k].append(cnt_1)
54                         new_mat_all_col[k].append(cnt_1_bk)
55                         #print "k:",k
56                         cnt_1 -= 1
57                     #print new_mat_col
58                 else:
59                     #new_mat_col[j] += "0"
60                     new_mat_col[j].append(0)
61                     new_mat_all_col[j].append(0)
62     return new_mat_row, new_mat_col, new_mat_all_col
63 class Solution(object):
64     def maximalSquare(self, matrix):
65         """
66         :type matrix: List[List[str]]
67         :rtype: int
68         """
69         if len(matrix)==0:
70             return 0
71         mat_row, mat_col, mat_all_col = get_1_0_matrix(matrix)
72         #print mat_row
73         #print mat_col
74         ans = 0
75         #print "rows =",len(mat), "cols =",len(mat[0])
76         for i in range(len(matrix)):
77             for j in range(len(matrix[i])):
78                 for length in range(mat_col[i][j],int(math.sqrt(ans)),-1):
79                     flag = -1
80                     for k in range(i,i+length):
81                         if mat_row[k][j]<length:
82                             flag = 1
83                             break
84                     if flag == -1:
85                         ans = max(ans, length**2)
86                         break
87         return ans
88
89 if __name__ == "__main__":
90     mat = ["1111111111111101001111111100111011111111","1111011011111111101101111101111111111111","0111101011111101101101101111111111111111","0101101011111111111101111111010110111111","1111111111110111110110010111111111111111","1111111110110101111111111101011111101111","0110110101110011111111111111110111110101","0111111111111100111111100110011011010101","1111011110111111111011011011110101101011","1111111110111111111101101101110111101111","1110110011111111111100111111111111111111","1011111110111101111001110111111111111111","0110111111111111101111110111011111011111","1111111111111111011111111111110111111011","1111100111111110101100111111111111101111","1111101111111110111111011111111111011111","1111101111111111111111011001111110011111","1111110111111111011111111111110111110111","1011111111111111010111110010111110111111","1111110011111111111110111111111111111011","1111111111111111110111011111011111011011","1100011011111111111111011111011111111111","1111101011111111111101100101110011111111","1110010111111111111011011110111101111101","1111111111111101101111111111101111111111","1111111011111101111011111111111110111111","1110011111111110111011111111110111110111","1111111111111100111111010111111111110111","1111111111111111111111000111111111011101","1111110111111111111111111101100111011011","1111011011111101101101111110111111101111","1111111111011111111111111111111111111111","1111111111111111111111111111111111111111","1100011111111110111111111111101111111011","1111101111111101111010111111111111111111","0111111111110011111111110101011011111111","1011011111101111111111101111111111110011","1010111111111111111111111111111110011111","0111101111011111111111111111110111111111","0111111011111111011101111011101111111111","0111111111110101111011111101011001111011","1111111111111011110111111101111111101110","1111101111111100111111111110111111001111","1101101111110101111101111111100111010100","0110111111100111110010111110111011011101"]
91     #mat = ["111","111","101"]
92     #mat = ["11110000","01111111","11111111","01111111","01111111","11111110","01111100","11111000"]
93     #new_mat_row, new_mat_col = get_1_0_matrix(mat)
94     #print new_mat_row
95     #print new_mat_col
96     s = Solution()
97     print s.maximalSquare(mat)

View Code

方法二:

dp[ i ][ j ]:表示以i ,j为右下角的正方形的边长

所以dp[i][j] = min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1])+1

 a   b 
 c   d

如上图中,abcd分别表示各个正方形的右下角,且边长分别为abcd。开始分类讨论abc的大小:

if a>=b>=c:

所以d=c+1, 因为a+1>=c+1(即d) & b+1>=c+1(即d),所以自然的构成了新的正方形

if b>=c>=a 和 if c>=b>=a 同理可证

 1 class Solution(object):
 2     def maximalSquare(self, matrix):
 3         """
 4         :type matrix: List[List[str]]
 5         :rtype: int
 6         """
 7         ans = 0
 8         res = []
 9         for i in range(0,len(matrix)):
10             #print "i:",i
11             res.append([])
12             for j in range(0,len(matrix[0])):
13                 #print "j:",j
14                 res[i].append(0)
15         for i in range(0,len(matrix)):
16             for j in range(0,len(matrix[0])):
17                 if i ==0 or j==0:
18                     if matrix[i][j]=='0':
19                         res[i][j] = 0
20                     else:
21                         res[i][j] = 1
22                 elif matrix[i][j]=='0':
23                     res[i][j] = 0
24                 else:
25                     res[i][j] = 1+min(res[i][j-1],min(res[i-1][j-1],res[i-1][j]))
26                 ans = max(ans,res[i][j])
27         return ans**2

View Code

转载于:https://www.cnblogs.com/xxx0624/p/5114684.html

Leetcode--221--Maximal Square相关推荐

  1. leetcode 221. Maximal Square | 221. 最大正方形(优化的暴力解法+动态规划解法)

    题目 https://leetcode.com/problems/maximal-square/ 题解 方法1:最暴力解 O((m*n)^2) public class Solution {publi ...

  2. LeetCode 221. Maximal Square (最大正方形)

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

  3. [leetcode ]221. Maximal Square c语言

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  4. LeetCode Top 100 Liked Questions 221. Maximal Square (Java版; Medium)

    welcome to my blog LeetCode Top 100 Liked Questions 221. Maximal Square (Java版; Medium) 题目描述 Given a ...

  5. LeetCode 221. Maximal Square----动态规划--谷歌面试算法题--Python解法

    题目地址:Maximal Square - LeetCode Given a 2D binary matrix filled with 0's and 1's, find the largest sq ...

  6. leetcode 85. Maximal Rectangle | 85. 最大矩形(单调栈)

    题目 https://leetcode.com/problems/maximal-rectangle/ 题解 本题与 leetcode 84. Largest Rectangle in Histogr ...

  7. leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题

    Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...

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

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

  9. 经典算法题之Maximal Square

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 作者:叶    虎 Maximal Square是道非常有意思的算 ...

  10. 【DP】LeetCode 85. Maximal Rectangle

    LeetCode 85. Maximal Rectangle Solution1: 一语惊醒梦中人啊,参考链接:https://www.youtube.com/watch?v=2Yk3Avrzauk ...

最新文章

  1. 最新 macOS Sierra 10.12.3 安装CocoaPods及使用详解
  2. 资源过于敏澸,8h删!这波福利....请笑纳。。
  3. Chrome浏览器官方离线安装包下载
  4. C#温故而知新学习系列之XML编程—Xml写入器XmlWriter类(三)
  5. 数据访问模式二:数据集和数据适配器(传统的数据访问模式)
  6. mysql的order by,group by和distinct优化
  7. 微信支付分使用用户数超2.4亿 每日使用笔数达千万级
  8. Beetl学习总结(3)——高级功能
  9. Java NPOIFSFileSystem.getRoot方法代碼示例
  10. centos7 服务器安装nginx,mysql,php
  11. 如何使用MySQL进行备份?
  12. 困惑度 | NLP中对困惑度感到困惑?
  13. 智能优化算法:学生心理学优化算法 -附代码
  14. CF16E Fish(状压+期望dp)
  15. 2017中国银行业发展趋势报告
  16. 电脑小知识:电脑怎么查看ip地址?
  17. 支付宝宣布:狗年还玩五福!全网第一份集福攻略在这里
  18. Getting Real ——把握现实
  19. 发布者订阅者模式之JAVA实现
  20. 同城货运烽火再燃,B端市场成货运市场新赛点

热门文章

  1. ICLR2021有什么值得关注的投稿?这些高赞论文先睹为快
  2. 百度AI快车道PaddleNLP实战营空降南京,11月9日技术大咖线下开讲
  3. 【Linux】最常用命令:简单易学,但能解决95%以上的问题
  4. 历史文章之python
  5. Python List pop()方法
  6. 吴恩达深度学习——提高算法运行效率
  7. 词性标注-隐马尔可夫模型
  8. 云计算将逐渐成为支撑SaaS应用的基础
  9. 4.5 Spark SQL 处理JSON数据
  10. spark概念、编程模型和模块概述