lintcode-436-最大正方形
436-最大正方形
在一个二维01矩阵中找到全为1的最大正方形
样例
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
返回 4标签
动态规划 爱彼迎 脸书
思路
使用动态规划,可以直接在 matrix 数组上修改,matrix[i][j] 表示以 i, j 为右下角的正方形的边的大小
- matrix[i][j] = 0 时,此点不能构成正方形,以 i, j 为右下角的正方形的边的大小为 0
- matrix[i][j] = 1 时,此点可以构成正方形,以 i, j 为右下角的正方形的边的大小为 min(以 i-1, j 为右下角的正方形的边的大小,以 i, j-1 为右下角的正方形的边的大小,以 i-1, j-1 为右下角的正方形的边的大小)+1
- 在遍历时同时保存最大边
- 正方形大小为边的平方
code
class Solution {
public:
/**
* @param matrix: a matrix of 0 and 1
* @return: an integer
*/
int maxSquare(vector<vector<int> > &matrix) {
// write your code here
int sizeRow = matrix.size();
if (sizeRow <= 0) {
return 0;
}
int sizeCol = matrix[0].size();
if (sizeCol <= 0) {
return 0;
}
int maxCount = matrix[0][0];
for (int i = 1; i < sizeRow; i++) {
for (int j = 1; j < sizeCol; j++) {
if (matrix[i][j] > 0 && matrix[i - 1][j] > 0 && matrix[i][j - 1] > 0 && matrix[i - 1][j - 1] > 0) {
matrix[i][j] = min(matrix[i - 1][j - 1], min(matrix[i - 1][j], matrix[i][j - 1])) + 1;
maxCount = max(maxCount, matrix[i][j]);
}
}
}
return maxCount * maxCount;
}
};
lintcode-436-最大正方形的更多相关文章
- 动态规划算法模板和demo
366. 斐波纳契数列 中文 English 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列 ...
- lintcode:最大子正方形
题目: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- [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 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- [LeetCode] Matchsticks to Square 火柴棍组成正方形
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
随机推荐
- 在Windows下编译mongo-c-driver 1.3.x
在Windows下编译mongo-c-driver 1.3.x 在Windows下编译mongo-c-driver 1.3.x 1.安装 MSYS2https://sourceforge.net/pr ...
- dtree的自定义select动作
项目中用到了dtree,别问我为什么用这么古老的插件,因为简单啊orz,文件树的条目不多,detree加载卡顿的问题也不用解决,开森. 在使用过程中在选择节点后需要自定义一些onclick的动作,本来 ...
- HDFS(0.20.2)运营中急救方案
这段时间折腾的都是hadoop和lucene,总结了hadoop在运营过程中出现问题时的解决方案,请大家指教! HDFS(0.20.2)运营中急救方案 1 Namenode 挂掉( ...
- 20155203 2016-2017-3 《Java程序设计》第4周学习总结
20155203 2016-2017-3 <Java程序设计>第4周学习总结 教材学习内容总结 1.父类和子类类似于集合和元素,不同的地方是子类可以拓展(extends)父类之外的方法. ...
- 20155210潘滢昊 2016-2017-2《Java程序设计》第一周学习总结
20155210 2016-2017-2<Java程序设计>第一周学习总结 教材学习内容总结 1.1.1: 本节主要讲了Java的由来,1995年5月23日是Java公认的诞生日.还有版本 ...
- 实验楼学习linux第一章第三节用户及文件权限管理
用户及文件权限管理 常用命令 查看用户 whoami 创建用户 sudo adduser 用户名 切换账户 su 用户名 删除账户 sudo deluser 用户名 --remove-home 查看用 ...
- 20155224 2016-2017-2 《Java程序设计》第3周学习总结
20155224 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 本周学习量比前两周大,代码量也比较多,还出现了挺多术语,都要慢慢查. 第四章 第四章主要学习 ...
- 20155327李百乾 Exp3 免杀原理与实践
20155327李百乾 Exp3 免杀原理与实践 实践guocheng 一.Msfvenom使用编码器 1.利用(virustota)[https://www.virustotal.com/]检测实验 ...
- Hibernate 3.0 HelloWorld
说明 基于Hibernate 3.0,Mysql5.0,java jdk 1.7,运行需要的lib 库,从http://files.cnblogs.com/HCCZX/Hibernate_Lib.ra ...
- Docker入门篇(二)之docker的单主机网络
Docker 安装时会自动在host上创建三个网络,我们可用 docker network ls命令查看: [root@localhost ~]# docker network ls NETWORK ...