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 ...
随机推荐
- circso 对数据进行可视化
circos可以用来绘制圈图,能够对染色体上的数据进行可视化,首先需要一个染色体的文件 染色体的文件如下,每列之间空格分隔 chr - chr1 chr1 chr - chr2 chr2 chr - ...
- 在GIT中创建一个空分支
ref: https://segmentfault.com/a/1190000004931751
- xshell-常用指令汇总 linux 常用指令
suse linux 常用命令 (1)命令ls——列出文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当前目录下以字母a开头的所有文件 l ...
- PHP截断函数mb_substr()
提示:mb_substr在于php中是默认不被支持的我们需要在在windows目录下找到php.ini打开编辑,搜索mbstring.dll,找到;extension=php_mbstring.dll ...
- ubuntu12.04 修改登陆用户 为root
Ubuntu 12.04默认是不允许root登录的,在登录窗口只能看到普通用户和访客登录.以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后,修改系统配置文件需要切换到超级用户模式,在终端 ...
- 使用Dreamweaver开发php
1.新建站点,开发的目录 2.服务器,服务器的目录 (并修改为“测试”) 3.必须结合WANP5
- [转]ASP.NET MVC 5 - 添加一个模型
在本节中,您将添加一些类,这些类用于管理数据库中的电影.这些类是ASP.NET MVC 应用程序中的"模型(Model)". 您将使用.NET Framework 数据访问技术En ...
- Oracle Merge Into Insert/Update
出自:http://blog.csdn.net/yuzhic/article/details/1896878 动机: 想在Oracle中用一条SQL语句直接进行Insert/Update的操作. 说明 ...
- 【RF库Collections测试】Dictionaries Should Be Equal
Name:Dictionaries Should Be EqualSource:Collections <test library>Arguments:[ dict1 | dict2 | ...
- Google's C++ coding style
v0.2 - Last updated November 8, 2013 源自 Google's C++ coding style rev. 3.274 目录 由 DocToc生成 头文件 ...