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就能找到.主要是 ...
随机推荐
- 获取当前目录下所有php文件内的函数名
$dir = dirname(__FILE__); $files = scandir($dir); foreach($files as $name){ if($name == '.' || $name ...
- laravel when 的用法
当你在使用where语句有前提条件时,比如某值为1的时候才执行where子句,否则不执行,这个时候,laravel5.5新出了一个简便方法when($arg,fun1[,fun2]). 具体用法如下: ...
- Composer管理thinkphp版本
安装Composer 下载 Composer 安装前请务必确保已经正确安装了 PHP.打开命令行窗口并执行 php -v 查看是否正确输出版本号. 打开命令行并依次执行下列命令安装最新版本的 Comp ...
- 笔记(assert 断言)
并发:在同一个时间段交替执行多个任务并行:在同一个时间点同时执行多个任务串行:同时执行的多个任务按顺序执行(换句话说就是一个任务执行完后才能执行下一个任务) #mysql limit用法: selec ...
- Go学习笔记01
前言 Go(Golang)是Google开发的一种强静态类型.编译型.并发型,并具有垃圾回收功能的编程语言,所以使用Go编写的程序相比nodejs之类的弱类型语言,可以提前在编译阶段发现错误,而且由于 ...
- 20155212 2016-2017-2 《Java程序设计》第5周学习总结
20155212 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 Chapter8 就Java在异常处理的设计上,不鼓励捕捉InputMismatchExce ...
- # 20155222卢梓杰 2016-2017-2 《Java程序设计》第2周学习总结
20155222卢梓杰 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 数据类型 所占字节数 short整数 2 int整数 4 long整数 8 float ...
- 20155230 实验二《Java面向对象程序设计》实验报告
20155230 实验二<Java面向对象程序设计>实验报告 一.单元测试 三种代码 知道了伪代码.产品代码.测试代码的关系和用途,并根据老师的例子,按测试代码调试了产品代码. 值得注意的 ...
- 20145209刘一阳《网络对抗》Exp8 Web基础
20145209刘一阳<网络对抗>Exp8 Web基础 基础问题回答 1.什么是表单? 表单是一个包含表单元素的区域,表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选框等等 ...
- WPF MVVM从入门到精通6:RadioButton等一对多控件的绑定
原文:WPF MVVM从入门到精通6:RadioButton等一对多控件的绑定 WPF MVVM从入门到精通1:MVVM模式简介 WPF MVVM从入门到精通2:实现一个登录窗口 WPF MVVM ...