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 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.
题目分析
我采用的方法比较笨拙,就是对于矩阵中的每一个元素,以反“L”型不断向右下扩增,以确定最大正方形的面积。
以下为个人实现(C++,25ms):
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
int row = matrix.size();
int max_a = 0;
for (int i = 0; i < row; i++) {
int col = matrix[i].size();
for (int j = 0; j < col; j++) {
if (matrix[i][j] - '0') {
int a;
for (a = 1; i + a < row && j + a < col; a++) { // overflow judge
int off_x, off_y;
// go through in 'L' shape
for (off_x = 0; off_x < a + 1 && matrix[i + a][j + off_x] - '0'; off_x++);
if (off_x < a + 1) {
break;
}
for (off_y = 0; off_y < a + 1 && matrix[i + off_y][j + a] - '0'; off_y++);
if (off_y < a + 1) {
break;
}
}
if (a > max_a) {
max_a = a;
}
}
}
}
return max_a * max_a;
}
};
毫无疑问这种解决方式太慢了。实际上我对很多点进行了重复判断,比如一个大正方形内部包含的若干小真方形,我都用“L”扩增的方式进行了判断,这种情况应该使用DP解决。
DP实现代码(C++,原贴):
int maximalSquare(vector<vector<char>>& matrix) {
if (matrix.empty()) return 0;
int m = matrix.size(), n = matrix[0].size();
vector<int> dp(m + 1, 0);
int maxsize = 0, pre = 0;
for (int j = 0; j < n; j++) {
for (int i = 1; i <= m; i++) {
int temp = dp[i];
if (matrix[i - 1][j] == '1') {
dp[i] = min(dp[i], min(dp[i - 1], pre)) + 1;
maxsize = max(maxsize, dp[i]);
}
else dp[i] = 0;
pre = temp;
}
}
return maxsize * maxsize;
}
如果有一个边长为n(n>=2)的正方形,那么它的内部一定存在4个边长为n-1的正方形。于是代码实现的思路是如果某元素值为1,那么取其左元素、上元素和左上元素中的边长最小值+1,得到的结果就可以作为该元素的边长,否则该元素边长为0。
LeetCode题解:(221) Maximal Square的更多相关文章
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- LeetCode 题解 593. Valid Square (Medium)
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-squa ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- [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 ...
随机推荐
- eclipse各种小图标含义
参考:https://wenku.baidu.com/view/69a0854df7ec4afe04a1df63.html outline: 实心的代表方法 空心的代表属性 绿色的圆表示公有pu ...
- mfc 类的友元函数
知识点 友元函数 友元函数 友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数..类授予它的友元特别的访问权.通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你 ...
- c++ 自定义类型,函数指针类型
用typedef定义函数指针类型 -函数指针和函数指针数组 46课里边有如下代码 int add(int a,int b,int d) { return a+b+d; } int mul(int a, ...
- Spring Boot:项目打包成war并发布到Tomcat上运行
一.修改pom文件 1. 因为SpringBoot中嵌入的有Tomcat,因此要移除嵌入式的Tomcat插件 <dependency> <groupId>org.springf ...
- 无聊中,写个常见的图片保护分类-iOS
当美工给出例如下面这种图的时候,但是需要拉伸的时候,就不得不做拉伸保护了. //调用代码 UIImage * img = [UIImage protectedImageWithLocalImageNa ...
- AWK高端功能-数组
第1章 awk命令基础 1.1 awk命令执行过程 1.如果BEGIN 区块存在,awk执行它指定的动作. 2.awk从输入文件中读取一行,称为一条输入记录.如果输入文件省略,将从标准输入读取 3.a ...
- python 模块之 bisect
python一个有趣的模块,bisect,感觉挺有趣,怎么有趣呢,下面来给你道来. 我们先生成一个list data=[4,8,7,1] data.sort() 打印这个list [1,4,7,8] ...
- Tomcat性能优化方案
1. 提高JVM栈内存Increase JVM heap memory 你使用过tomcat的话,简单的说就是"内存溢出". 通常情况下,这种问题出现在实际的生产环境中.产生这种问 ...
- facebook和twitter的截图分享
记录一下代码,以后自己可以抄..我抄我自己=.= SDK的接入看之前的博客.所以话不多少,直接上代码(记得分享一定要安装原生app(FB和twitter),不然只通过网页分享无法发图片.),初始化都在 ...
- ETSI公布的多接入移动边缘计算概念验证
ETSI多接入移动边缘计算 公布的概念验证如下: 来源 MEC PoC Projects PoC#1: "Video User Experience Optimization via MEC ...