【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/maximal-square/description/
题目描述
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.
Example:
Input:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Output: 4
题目大意
给出了一个二维的数组,求在这里面能够成的最大的正方形面积是多少。
解题方法
动态规划
有两种方法,第一种就是对矩阵的每个位置求左上角以上所有元素的和,然后用所有可能构成的正方形的区域内进行面积计算,如果面积等于正方形边长的平方,说明是一个正方形,然后求最大面积。
第二种方法使用DP。设这个DP[i][j]数组为以i, j位置为右下角顶点的能够成的最大正方形的边长。数组如果是第一行或者第一列,显然dp和matrix相等。如果是其他位置,当matrix[i][j] = 1时,能够成的正方形等于左边、上边、左上能够成的正方形边长的最小值+1.为什么是最小值?因为只要存在一个0,那么就没法构成更大的正方形,这个是很保守的策略。
递推公式如下:
- dp[0][j] = matrix[0][j] (topmost row);
- dp[i][0] = matrix[i][0] (leftmost column);
- For i > 0 and j > 0: if matrix[i][j] = 0, dp[i][j] = 0; if matrix[i][j] = 1, dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1.
用图来说:
时间复杂度是O(N2),空间复杂度是O(N2)。
class Solution(object):
def maximalSquare(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if not matrix: return 0
M = len(matrix)
N = len(matrix[0])
dp = [[0] * N for _ in range(M)]
for i in range(M):
dp[i][0] = int(matrix[i][0])
for j in range(N):
dp[0][j] = int(matrix[0][j])
for i in range(1, M):
for j in range(1, N):
if int(matrix[i][j]) == 1:
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1
return max(map(max, dp)) ** 2
C++代码如下:
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if (matrix.size() == 0 || matrix[0].size() == 0) return 0;
const int M = matrix.size(), N = matrix[0].size();
vector<vector<int>> dp(M, vector<int>(N, 0));
int res = 0;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (i == 0 || j == 0)
dp[i][j] = matrix[i][j] - '0';
else if (matrix[i][j] == '1')
dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
res = max(res, dp[i][j]);
}
}
return res * res;
}
};
参考资料:
https://leetcode.com/problems/maximal-square/discuss/61935/6-lines-Visual-Explanation-O(mn)
https://www.youtube.com/watch?v=vkFUB--OYy0
日期
2018 年 10 月 10 日 —— 冻成狗
2019 年 1 月 11 日 —— 小光棍节?
【LeetCode】221. Maximal Square 解题报告(Python & C++)的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [LeetCode] 221. Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- Java for LeetCode 221 Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- (medium)LeetCode 221.Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- Leetcode 221. Maximal Square
本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
随机推荐
- 架构B/S和C/S的区别
CS = Client - Server = 客戶端 - 服務器.例子: QQ,迅雷,快播,暴風影音,各種網絡遊戲等等.只要有和服務器通訊的都算. CS(Client/Server):客户端----服 ...
- .Net Core——用SignalR撸个游戏
之前开内部培训,说到实时web应用这一块讲到了SignalR,我说找时间用它做个游戏玩玩,后面时间紧张就一直没安排.这两天闲了又想起这个事,考虑后决定用2天时间写个斗D主,安排了前端同学写客户端,我写 ...
- 第二个基础框架 — spring — xml版,没用注解 — 更新完毕
1.什么是spring? 老规矩:百度百科一手 这上面说得太多了,我来提炼一下: spring就是一个轻量级的控制反转( IOC ) 和 面向切面编程( AOP ) 的容量框架.总的来说:本质就是对j ...
- C/C++ Qt 数据库与TreeView组件绑定
在上一篇博文<C/C++ Qt 数据库QSql增删改查组件应用>介绍了Qt中如何使用SQL操作函数,并实现了对数据库的增删改查等基本功能,从本篇开始将实现数据库与View组件的绑定,通过数 ...
- accelerate
accelerate accelerare, accumulare和accurate共享一个含义为to的词根,后半截分别是:fast, pile up, care (关心则精确). 近/反义词: ex ...
- ASP.NET Core中使用固定窗口限流
算法原理 固定窗口算法又称计数器算法,是一种简单的限流算法.在单位时间内设定一个阈值和一个计数值,每收到一个请求则计数值加一,如果计数值超过阈值则触发限流,如果达不到则请求正常处理,进入下一个单位时间 ...
- Insert into select语句引发的生产事故
前言 Insert into select请慎用.这天xxx接到一个需求,需要将表A的数据迁移到表B中去做一个备份.本想通过程序先查询查出来然后批量插入.但xxx觉得这样有点慢,需要耗费大量的网络 ...
- ReactiveCocoa操作方法-重复
retry重试 只要失败,就会重新执行创建信号中的block,直到成功. __block int i = 0; [[[RACSignal createSignal:^RACDisposabl ...
- Can references refer to invalid location in C++?
在C++中,引用比指针更加的安全,一方面是因为引用咋定义时必须进行初始化,另一方面是引用一旦被初始化就无法使其与其他对象相关联. 但是,在使用引用的地方仍然会有一些例外. (1)Reference t ...
- 【Linux】【Services】【Package】Basic
Linux程序包管理 概述 API:Application Program Interface ABI:Application Binary Int ...