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的更多相关文章

  1. 求解最大正方形面积 — leetcode 221. Maximal Square

    本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...

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

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

  4. (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 ...

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

  6. Leetcode 221. Maximal Square

    本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...

  7. leetcode每日解题思路 221 Maximal Square

    问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...

  8. 【动态规划】leetcode - Maximal Square

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

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

随机推荐

  1. 【Java面试题】2 Java中使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变?超详细解析

    /* * 问题:使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变 * 答: * 使用final关键字修饰一个变量时,是指引用变量不能变,引用变量所指向的对象中的内容还是可以改变的 ...

  2. CPU性能判断指标---上下文切换,运行队列和使用率

    http://blog.chinaunix.net/uid-15007890-id-3064254.html uptime11:35:08 up 21:57,  6 users,  load aver ...

  3. Buff系统框架设计

    Buff的配置文件 BufType: 1: 精神类Buf 2: 物理类Buf 3.元素类Buf 4.其他类Buf 5.被动类BufBufSubType: 1000-1999 精神子类 2000-299 ...

  4. android 开发者资源下载地址记录(转+补充)

    https如果无法下载的话将下面的:https://dl-ssl 部分改为 http://dl (1)Android SDK (Android SDK主安装包,包含SDK Manager.AVD Ma ...

  5. 【java】java内存模型 (1)--基础

    并发编程模型的分类 在并发编程中,我们需要处理两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实体).通信是指线程之间以何种机制来交换信息.在命令式编程中,线程之间的通信 ...

  6. 安装并配置ROS环境1

    ros学习之路(原创博文,转载请标明出处-周学伟http://www.cnblogs.com/zxouxuewei/) 一.ros核心教程    1.安装并配置ROS环境: 注意: 学习这节课之前请按 ...

  7. DUBBO功能使用说明

    DUBBO功能使用说明 1 DUBBO概述 DUBBO是阿里巴巴公司的一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 相比于其他服务框架,DUBBO有如 ...

  8. layui时间,table,大图查看,弹出框,获取音频长度,文件上传

    1.引入: <link href="../../Scripts/layui-v2.3.0/css/layui.css" rel="stylesheet" ...

  9. javascript--枚举算法实现

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. mybatis由浅入深day02_4多对多查询_多对多查询总结

    4 多对多查询 4.1 需求(查询用户及用户购买商品信息) 查询用户及用户购买商品信息. 4.2 sql语句 查询主表是:用户表 关联表:由于用户和商品没有直接关联,通过订单和订单明细进行关联,所以关 ...