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-最大正方形的更多相关文章

  1. 动态规划算法模板和demo

    366. 斐波纳契数列 中文 English 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: 前2个数是 0 和 1 . 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列 ...

  2. lintcode:最大子正方形

    题目: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  3. [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 ...

  4. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  6. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  7. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  8. [LeetCode] Matchsticks to Square 火柴棍组成正方形

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  9. [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 ...

  10. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

随机推荐

  1. Leecode刷题之旅-C语言/python-231 2的幂

    /* * @lc app=leetcode.cn id=231 lang=c * * [231] 2的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  2. PhpStorm2016.2版本 安装与破解

    1.PhpStorm2016简介以及下载地址 1.1.PhpStorm介绍    PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率,可深刻理解用户的编码,提供智能代码补全 快速导 ...

  3. 20155332 2016-2017-2《Java程序设计》第1周学习总结

    # 20155332 2016-2017-2<Java程序设计>第1周学习总结 教材学习内容总结 课本提问 1.Java平台概论:为什么需要Java程序设计语言? 2.Java平台概论:版 ...

  4. 【代码沉淀】 - EPPlus - 操作xlsx表格文件

    EPPlus - Create advanced Excel spreadsheets on the server.web: http://epplus.codeplex.com/nuget: Ins ...

  5. PHP 行为测试工具 Codeception (介绍)

    原文地址:https://phphub.org/topics/25 Codeception 简介 Codeception 简单来说, 分为以下几种测试 Acceptance Tests 验收测试 Fu ...

  6. 2017"百度之星"程序设计大赛 - 初赛(A) 小C的倍数问题

    谢谢帮忙刷访问量! 题解当然下考再发啦 答案为P-1的约数个数 // It is made by XZZ #include<cstdio> #include<algorithm> ...

  7. 高能福利 |"荐"者有份,有"福"同享

    WeTest 导读 越来越多的开发者加入WeTest大家庭了,感谢大家一直以来的支持,WeTest又有新一步“大福利”赠送了! 即日起,参加WeTest用户推荐有礼活动,推荐者和被推荐者皆可获得福利. ...

  8. 一个IP可以登几个拼多多后台 拼多多如何推广营销

    想要在拼多多上开双店?担心一根网线会引起IP冲突?那么一根网线可以登几个拼多多后台呢?有数据显示,挂双店是没有问题的,甚至可以多开.那么解决了一根网线的事情,要怎么对自己的店铺进行营销推广呢?下面是小 ...

  9. HTML5 + CSS3 实现地球绕太阳公转

    使用的是正面视角,主要是用 HTML5 + CSS3 来实现,JS只是用来画图. test.html: <!DOCTYPE html> <html> <head> ...

  10. Phaser3游戏三角学应用--一只跟随屏幕点击位置游动的鱼

    fish fish 资源图: fish-136x80.png undersea-bg.png 代码 var config = { type: Phaser.AUTO, parent: 'iFiero' ...