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 ...
随机推荐
- Redis基础(一)——字符串、hash类型的基本使用
day09--Redis Redis介绍和安装 # Redis:软件,存储数据的,速度非常快,Redis是一个key-value存储系统(没有表的概念),cs架构的软件 服务端 客户端(python作 ...
- 使用C# 创建、填写、删除PDF表单域
通常情况下,PDF文件是不可编辑的,但PDF表单提供了一些可编辑区域,允许用户填写和提交信息.PDF表单通常用于收集信息.反馈或进行在线申请,是许多行业中数据收集和交换的重要工具. PDF表单可以包含 ...
- 力扣184(MySQL)-部门工资最高的员工(中等)
题目: 表: Employee 表: Department 编写SQL查询以查找每个部门中薪资最高的员工.按 任意顺序 返回结果表.查询结果格式如下例所示. 解题思路: 方法一:窗口函数和多表联结 ...
- 拥抱云原生,Fluid结合JindoFS :阿里云OSS加速利器
简介: Fluid 是一个开源的 Kubernetes 原生的分布式数据集编排和加速引擎,主要服务于云原生场景下的数据密集型应用.在 Fluid 上使用和部署 JindoRuntime 实现数据集的可 ...
- Java异步非阻塞编程的几种方式
简介: Java异步非阻塞编程的几种方式 一. 从一个同步的Http调用说起 一个很简单的业务逻辑,其他后端服务提供了一个接口,我们需要通过接口调用,获取到响应的数据. 逆地理接口:通过经纬度获取这个 ...
- 冬奥幕后故事:从低碳火炬到AI裁判,十四年后中国科技再上场
北京冬奥会开幕后,一个段子在社交媒体上流传甚广:"夏奥开幕式和冬奥开幕式就差半年,这半年人类科技进步真大啊." 文|张婧怡 封面来源|北京日报客户端 冬奥季终于到来. 2月4 ...
- Java单元测试技巧之PowerMock
简介: 高德的技术大佬向老师在谈论方法论时说到:"复杂的问题要简单化,简单的问题要深入化." 这句话让我感触颇深,这何尝不是一套编写代码的方法--把一个复杂逻辑拆分为许多简单逻辑, ...
- [BlockChain] 三方互惠是公共区块链得以发展的基石, dApp数字通证的运转需要可持续性玩法
------------------------------- 公链 旷工 开发者/用户 ------------------------------- -------------------- ...
- [PHP] Laravel 依赖注入使用不当引起的内存溢出
业务逻辑: 正常在 controller 方法的参数中注入某个类,方法中使用这个类时发生内存超出提示. 分析: 过往显示,正常使用依赖注入是不存在问题的,那么很有可能是哪里发生了循环引用,导致一直请求 ...
- vue-cli快速搭建项目的几个文件(一)
===========app.vue文件============= <template> <div id="app"> <router ...