[LintCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 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
.
LeetCode上的原题,请参见我之前的博客Maximal Square。
解法一:
class Solution {
public:
/**
* @param matrix: a matrix of 0 and 1
* @return: an integer
*/
int maxSquare(vector<vector<int> > &matrix) {
if (matrix.empty() || matrix[].empty()) return ;
int m = matrix.size(), n = matrix[].size(), res = ;
vector<vector<int>> sum = matrix;
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
int t = sum[i][j];
if (i > ) t += sum[i - ][j];
if (j > ) t += sum[i][j - ];
if (i > && j > ) t -= sum[i - ][j - ];
sum[i][j] = t;
int cnt = ;
for (int k = min(i, j); k >= ; --k) {
int d = sum[i][j];
if (i - cnt >= ) d -= sum[i - cnt][j];
if (j - cnt >= ) d -= sum[i][j - cnt];
if (i - cnt >= && j - cnt >= ) d += sum[i - cnt][j - cnt];
if (d == cnt * cnt) res = max(res, d);
++cnt;
}
}
}
return res;
}
};
[LintCode] Maximal Square 最大正方形的更多相关文章
- [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 ...
- [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 ...
- 221 Maximal Square 最大正方形
在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积.例如,给出如下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 4. 详见:https://l ...
- Leetcode221. Maximal Square最大正方形
在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 方法一 ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- lintcode:最大子正方形
题目: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
随机推荐
- UIPopoverController 的使用
#import "ViewController.h" #import "RYColorSelectController.h" #import "RYM ...
- centos7安装redis3.2.5
安装redis 1官方介绍 Installation Download, extract and compile Redis with: $ wget http://download.redis.io ...
- 【转】清理Kylin的中间存储数据(HDFS & HBase Tables)
http://blog.csdn.net/jiangshouzhuang/article/details/51290399 Kylin在创建cube过程中会在HDFS上生成中间数据.另外,当我们对cu ...
- web_custom_request应用示例
web_custom_request应用示例 LoadRunner提供的web_custom_request函数可以用于实现参数的动态生成.在LoadRunner中,web_reg_save_para ...
- 2016.8.22 JavaScript入门之三
1.对一个数组的末尾追加数据的一种简便方法是通过push()功能. 例如: var myArray = [["John", 23], ["cat", 2]];m ...
- POJ 2002 统计正方形 HASH
题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...
- 模数转换(A/D)和数模转换(D/A) 图示
(从书中截图) 在时间域和频率域中示意图: 1.A/D转换 2.D/A转换
- express-9 Handlebars模板引擎(2)
视图和布局 视图通常表现为网站上的各个页面(它也可以表现为页面中AJAX局部加载的内容,或一封电子邮件,或页面上的任何东西).默认情况下,Express会在views子目录中查找视图.布局是一种特殊的 ...
- css之overflow
也说css之overflow:细探之下有意想不到的结果 2016-11-5 滴滴出行·DDFE 作者:dolymood overflow 是一个非常常用的 CSS 属性,一般来说会认为很简单,其实细究 ...
- C# 开发积累(1)
EntityFramework批量增加时报"...请在调用 AcceptChanges 之前,确保键值是唯一的" http://www.xinglongjian.com/i ...