LeetCode 221. Maximal Square 最大正方形(C++/Java)
题目:
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
Example:
Input: 1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0 Output: 4
分析:
给定一个二维数组,其中的元素是0或者1,求最大的正方形的面积(1的个数),正方形是指内部全为1的区域。
定义一个新的二维数组dp,dp[i][j]表示以(i, j)元素为右下角的最大正方形的边长。dp[i][j]受到dp[i-1][j],dp[i][j-1],dp[i-1][j-1]影响,以下面为例说明。
1 1 1 1 1 1
1 1 1 1 2 2
0 1 1 0 1 ?
左侧是所给的二维数组matrix,右侧是我们求得的dp数组,此时我们要求dp[2][2],也就是以matrix[2][2]为右下角的最大正方形的边长,实际上它收到左方上方和左上方的值影响,因为左侧元素上侧元素和左上侧元素同样分别是以他们各自为右下角元素的最大正方形边长,而如果当前位置的元素为1的话,意味着这个元素可以和那三个元素拼接成更大的正方形,所以在他们中选取最小的值加1,便是dp[2][2]的值,?处应填2,实际上:
if(matrix[i][j] == '1')
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
程序:
C++
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.empty())
return 0;
int m = matrix.size();
int n = matrix[0].size();
vector<vector<int>> dp(m, vector<int>(n, 0));
int result = 0;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(matrix[i][j] == '0')
continue;
if(i == 0 || j == 0){
dp[i][j] = matrix[i][j] - '0';
}
else{
dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
}
result = max(dp[i][j] * dp[i][j], result);
}
}
return result;
}
};
Java
class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix.length == 0 || matrix == null)
return 0;
int m = matrix.length;
int n = matrix[0].length;
int[][] dp = new int[m][n];
int result = 0;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(matrix[i][j] == '0')
continue;
if(i == 0 || j == 0){
dp[i][j] = matrix[i][j] - '0';
}
else{
dp[i][j] = Math.min(Math.min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1;
}
result = Math.max(dp[i][j] * dp[i][j], result);
}
}
return result;
}
}
LeetCode 221. Maximal Square 最大正方形(C++/Java)的更多相关文章
- [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
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- 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 ...
- 221 Maximal Square 最大正方形
在一个由0和1组成的二维矩阵内,寻找只包含1的最大正方形,并返回其面积.例如,给出如下矩阵:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0返回 4. 详见:https://l ...
- (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
本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...
- [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
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
随机推荐
- 如何在 Anolis 8上部署 Nydus 镜像加速方案?
简介: 手把手教你在 Anolis OS 上部署 Nydus! 在上一篇文章中详细介绍Anolis OS 是首个原生支持镜像加速 Linux 内核,Nydus 镜像加速服务重新优化了现有的 OCIv1 ...
- 平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道
简介:一端是算力无穷的云,这也是 CloudXR 的精髓所在. 图:2022阿里云视觉计算私享会现场 5月11日,在"2022阿里云视觉计算私享会"上,平行云CEO李岩为大家 ...
- 平安保险基于 SPI 机制的 RocketMQ 定制化应用
简介:本文讲讲述平安保险为何选择 RocketMQ,以及在确定使用消息中间件后,又是如何去选择哪款消息中间件的. 作者:孙园园|平安人寿资深开发 为什么选用 RocketMQ 首先跟大家聊聊我们为什 ...
- 作业帮基于 DeltaLake 的数据湖建设最佳实践
简介:作业帮是一家以科技为载体的在线教育公司,其大数据中台作为基础系统中台,主要负责建设公司级数仓,向各个产品线提供面向业务主题的数据信息.本文主要分享了作业帮基于 DeltaLake 的数据湖建设 ...
- Alibaba Cloud Toolkit 中SLS插件助力线上服务问题排查
简介:Alibaba Cloud Toolkit 是一款非常优秀的插件,新增SLS日志服务的功能,针对软件开发者日常工作中常见的问题排查场景,将日志服务平台的功能集成到ide当中,省去了不同窗口之间 ...
- Spark 大数据处理最佳实践
开源大数据社区 & 阿里云 EMR 系列直播 第十一期 主题:Spark 大数据处理最佳实践 讲师:简锋,阿里云 EMR 数据开发平台 负责人 内容框架: 大数据概览 如何摆脱技术小白 Spa ...
- UNO 的 SamplesApp.Skia.Gtk 丢失字体文件抛出空异常
在运行 UNO 的 SamplesApp.Skia.Gtk 例子程序时,如果没有拷贝字体文件夹,导致字体丢失,将会在运行的时候抛出 NullReferenceException 空异常 抛出的异常堆栈 ...
- 一些简单的Post方式WebApi接收参数和传递参数的方法及总结
原文地址:https://www.zhaimaojun.top/Note/5475297(我自己的博客网站) 各种Post方式上传参数到服务器,服务器接收各种参数的示例 webapi可以说是很常用了, ...
- golang url解析
package main import "fmt" import "net/url" import "strings" func main( ...
- layui.js
目录 用法: 1.在base.js里导入layui插件 2.在使用的html页面里引入 base.js lucky.js index.html 用法: 1.在base.js里导入layui插件 2.在 ...