leetcode之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.
动态规划求解:
设dp[i][j]表示以matrix[i][j]结尾的最大正方形,则初始化:dp[i][j]=matrix[i][j],0<=i<=m,0<=j<=n(m,n为matrix的行数和列数)
状态转移方程:
dp[i][j] = MIN(dp[i-1][j-1],dp[i-1][j],dp[i][j-1])+1,if matrix[i-1][j-1] == '1' and matrix[i][j] == '1' and matrix[i-1][j] == '1' and matrix[i][j-1] == '1'
代码:
class Solution {
#define MIN(a,b,c) (a)<=(b)?((a)<=(c)?(a):(c)):((b)<=(c)?(b):(c))
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty())
{
return 0;
}
int m = matrix.size();
int n = matrix[0].size();
int dp[m+1][n+1];
int max = 0;
for(int i = 0;i < m;i ++)
{
for(int j = 0;j < n;j++)
{
if(matrix[i][j] == '1')
dp[i][j] = 1;
else
dp[i][j] = 0;
if(max < dp[i][j])
max = dp[i][j];
}
}
for(int i = 1;i < m;i ++)
{
for(int j = 1;j < n;j++)
{
if(matrix[i-1][j-1] == '1' && matrix[i][j] == '1' && matrix[i-1][j] == '1' && matrix[i][j-1] == '1')
{
int n = MIN(dp[i-1][j-1],dp[i-1][j],dp[i][j-1]);
dp[i][j] = n + 1;
}
else
{
}
if(max < dp[i][j])
max = dp[i][j];
}
}
return max * max;
}
};
leetcode之Maximal Square的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [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 ...
- (medium)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 ...
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- Leetcode 221. Maximal Square
本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...
- 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 ...
随机推荐
- 【问题】报错[CRITICAL] Rendering SLS 'base:minions.install' failed: Jinja variable 'list' object has no element 0
1.报错[CRITICAL] Rendering SLS 'base:minions.install' failed: Jinja variable 'list' object has no elem ...
- Linux(Ubuntu)下搭建ASP.NET Core环境
今天来学习一下ASP.NET Core 运行在Ubuntu中.无需安装mono . 环境 Ubuntu 14.04.4 LTS 服务器版 全新安装系统. 下载地址:http://mirrors.neu ...
- js使用正则表达式从url中获取参数值
//从url中获取参数值 function getvl(name) { var reg = new RegExp("(^|\\?|&)"+ name +"=([^ ...
- iOS10.0 & Swift 3.0 对于升级项目的建议
iOS & Swift新旧版本更替, 在Apple WWDC大会开始之际, 也迎来了iOS 10.0, Swift 3.0 测试版, 到目前为止, 已经是测试版2.0, 每次更新都带来了新的语 ...
- Unity文件操作路径
Unity3D中的资源路径: Application.dataPath:此属性用于返回程序的数据文件所在文件夹的路径.例如在Editor中就是Assets了. Application.streamin ...
- MVC 5 视图之公用代码
一.公共模板 1.@RenderBody() 在网站公用部分通过一个占位符@RenderBody()来为网站独立部分预留一个位置.然后私有页面顶部通过@{Layout="公用模板路径&quo ...
- 安装ionice v2版本(官方帮助文档)
安装最新的 ionic 命令行工具 npm install -g ionic@latest 官方文档:http://ionicframework.com/docs/v2/getting-started ...
- json 数据分析
/* 健一健康头条 */ try { String url = "http://www.j1health.com/j1api.php/index/getJ1healthHotLists&qu ...
- STM32的操作过程,寄存器配置与调试过程(转载)
很多学习stm32的,为什么学习stm32他也不知道,我们所知道的就是各个论坛讨论stm32的很多,而我们很多人之所以学习stm32是很多的淘宝卖家做了大量的图片文字宣传,于是我们经不住诱惑就买了板子 ...
- iOS - UIImageView - how to handle UIImage image orientation
本文转载至 http://stackoverflow.com/questions/8915630/ios-uiimageview-how-to-handle-uiimage-image-orienta ...