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 ...
随机推荐
- Spring第二天——IOC注解操作与AOP概念
大致内容 spring的bean管理(注解实现) AOP原理 log4j介绍 spring整合web项目的演示 一.spring注解实现bean管理 注解: 代码中一些特殊的标记,使用注解也可以完成一 ...
- 20155327Exp2 后门原理与实践
20155327Exp2 后门原理与实践 一.实验说明 任务一:使用netcat获取主机操作Shell,cron启动 (0.5分) 任务二:使用socat获取主机操作Shell, 任务计划启动 (0. ...
- python基础学习1-面向对象
#!/usr/bin/env python # -*- coding:utf-8 -*- class Foo:#定义类 def mail(self,email,message):#定义类的方法 pri ...
- Ubuntu14.04 64位机上安装OpenCV2.4.13(CUDA8.0)版操作步骤
Ubuntu14.04 64位机上安装CUDA8.0的操作步骤可以参考http://blog.csdn.net/fengbingchun/article/details/53840684,这里是在已经 ...
- 【mysql】排序方法
查询各科成绩前三名的记录,不考虑并列的情况: select a.course_id as 课程ID, a.score as 成绩, count(a.course_id) as 排名 from scor ...
- [CF1027F]Session in BSU[最小基环树森林]
题意 有 \(n\) 门课程,每门课程可以选择在 \(a_i\) 或者 \(b_i\) 天参加考试,每天最多考一门,问最早什么时候考完所有课程. \(n\leq 10^6\). 分析 类似 [BZOJ ...
- [Luogu1891]疯狂LCM[辗转相减法]
题意 多组询问,每次给定 \(n\) ,求:\(\sum_{i=1}^nlcm(i,n)\) . \(\rm T \leq 3\times 10^4\ ,n \leq 10^6\). 分析 推式子: ...
- Design3:数据层次结构建模之二
SQL Server提供了一个新的数据类型 HierarchyID,用来处理层次结构的数据,这个数据类型是系统内置的CLR数据类型,不需要专门激活 SQL/CLR 功能即可使用.当需要表示各值之间的嵌 ...
- Cocos2DX开发:记录遇到的一些问题和解决方法
今天看了一下以前学习cocos2dx时记录的一些笔记,主要是在实际中遇到的一些问题,整理了一下,就成为了这篇文章,便于自己以后查找,也为一些新手提供点经验. 这篇文章会一直更新,将自己之后开发中遇到的 ...
- bintray 在android3.2上传遇到的问题
1.报错信息如下: Gradle DSL method not found: 'google()'Possible causes: The project 'JustTest' may be usin ...