【动态规划】leetcode - Maximal Square

时间:2022-08-31 14:11:12

称号:

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.

分析:

利用动态规划求解。建立一个类node。node中成员变量left记录每个点的左边有几个1(包含该点本身)、up记录上边有几个1(包含该点本身)、maxsize记录该点相应的最大正方形的边长(该点在正方形右下角)。若一个点是‘0’,则其相应的node是(0,0,0).

1、用变量res记录最大正方形的边长。

2、先依次处理输入矩阵matrix左上角那个点、第一行和第一列,求出这些位置的node值。

3、再依次遍历matrix剩下的点,对每个点求出node值。并更新res。

4、返回res*res.

class node
{
public:
int left,up,maxsize;
node():left(0),up(0),maxsize(0){}
node(int a,int b,int c):left(a),up(b),maxsize(c){}
}; class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty() || matrix[0].empty())
return 0; int rows=matrix.size(),cols=matrix[0].size();
int res=0;
vector<vector<node>> dp(rows,vector<node>(cols)); if(matrix[0][0]=='1')
{
res=1;
dp[0][0]=node(1,1,1);
}
for(int j=1;j<cols;++j)
{
if(matrix[0][j]=='1')
{
res=1;
dp[0][j]=node(dp[0][j-1].left+1,1,1);
}
}
for(int i=1;i<rows;++i)
{
if(matrix[i][0]=='1')
{
res=1;
dp[i][0]=node(1,dp[i-1][0].up+1,1);
}
} for(int i=1;i<rows;++i)
{
for(int j=1;j<cols;++j)
{
if(matrix[i][j]=='1')
{
dp[i][j].left=dp[i][j-1].left+1;
dp[i][j].up=dp[i-1][j].up+1;
if(matrix[i-1][j-1]!='1')
dp[i][j].maxsize=1;
else
{
int tmp=min(dp[i-1][j-1].maxsize+1,dp[i][j].left);
tmp=min(tmp,dp[i][j].up);
dp[i][j].maxsize=tmp;
}
res=max(res,dp[i][j].maxsize);
}
}
} return res*res;
}
};

版权声明:本文博主原创文章,博客,未经同意不得转载。

【动态规划】leetcode - Maximal Square的更多相关文章

  1. LeetCode之&OpenCurlyDoubleQuote;动态规划”:Maximal Square &amp&semi;&amp&semi; Largest Rectangle in Histogram &amp&semi;&amp&semi; Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  2. &lbrack;LeetCode&rsqb; 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. LeetCode Maximal Square

    原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...

  4. 求解最大正方形面积 — leetcode 221&period; Maximal Square

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

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

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

  6. 【LeetCode】221&period; Maximal Square

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

  7. &lbrack;LeetCode&rsqb; Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  8. &lbrack;LintCode&rsqb; Maximal Square 最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  9. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

随机推荐

  1. 如何保存联系人到系统通讯录&lpar;android&rpar;

    1 效果演示: 2 代码演示:

  2. SQL高级查询之一

    一,子查询 SELECT e.emp_id, e.fname, e.lname FROM (SELECT emp_id, fname, lname, start_date, title FROM em ...

  3. Xamarin开发笔记—WebView双项事件调用

    1.Xamarin调用WebView: 原理:Xamarin.Forms WebView内置方法xx.Eval(..)可以调用到页面里面的js函数. WebView展示的代码如下: var htmlS ...

  4. JVM内存分析

    1.java内存模型分析 java虚拟机运行时数据存储区域包括线程隔离和线程共享两类,整个PC的内存图如下所示: 下面对以上内存区域说明: 1.1 register和cache 当代计算机一般有多个c ...

  5. JAVA程序调试

    调试 步骤1:设置断点(不能在空白处设置断点) 步骤2:启动调试 步骤3:调试代码(F6单步跳过)笔记本Fn+F6(F5) 步骤4:结束调试 掌握调试的好处? l  很清晰的看到,代码执行的顺序 l  ...

  6. Go语言中的RPC调用

    首先,说一下目录结构: 一.HttpRPC 1.建立服务文件 /*Go RPC的函数只有符合下面的条件才能被远程访问,不然会被忽略,详细的要求如下: 函数必须是导出的(首字母大写) 必须有两个导出类型 ...

  7. iOS绘图UIBezierPath 和 Core Graphics框架

    前言 iOS系统本身提供了两套绘图的框架,即UIBezierPath 和 Core Graphics.而前者所属UIKit,其实是对Core Graphics框架关于path的进一步封装,所以使用起来 ...

  8. 114&period; Unique Paths &lbrack;by Java&rsqb;

    Description A robot is located at the top-left corner of a m x n grid. The robot can only move eithe ...

  9. How to ignore SSL certificate errors in Apache HttpClient 4&period;4

    public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, NoSuch ...

  10. HDU 2859 Phalanx (DP)

    Phalanx Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...