LeetCode: 221_Maximal Square | 二维0-1矩阵中计算包含1的最大正方形的面积 | Medium
题目:
Given a 2D binary matrix filled with 's and 1's, find the largest square containing all 's and return its area. For example, given the following matrix: Return .
解题思路:
这种包含最大、最小等含优化的字眼时,一般都需要用到动态规划进行求解。本题求面积我们可以转化为求边长,由于是正方形,因此可以根据正方形的四个角的坐标写出动态规划的转移方程式(画一个图,从左上角推到右下角,很容易理解):
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1; where matrix[i][j] == 1
根据此方程,就可以写出如下的代码:
代码展示:
#include <iostream>
#include <vector>
#include <cstring>
using namespace std; //dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1;
//where matrix[i][j] == 1
int maximalSquare(vector<vector<char>>& matrix)
{
if (matrix.empty())
return ; int rows = matrix.size();//行数
int cols = matrix[].size(); //列数 vector<vector<int> > dp(rows+, vector<int>(cols+, ));
/*
0 0 0 0 0 0
0 1 0 1 0 0
0 1 0 1 1 1
0 1 1 1 1 1
0 1 0 0 1 0
*/
int result = ; //return result for (int i = ; i < rows; i ++) {
for (int j = ; j < cols; j ++) {
if (matrix[i][j] == '') {
int temp = min(dp[i][j], dp[i][j+]);
dp[i+][j+] = min(temp, dp[i+][j]) + ;
}
else
dp[i+][j+] = ; result = max(result, dp[i+][j+]);
}
}
return result * result; //get the area of square;
} // int main()
// {
// char *ch1 = "00000";
// char *ch2 = "00000";
// char *ch3 = "00000";
// char *ch4 = "00000";
// vector<char> veccol1(ch1, ch1 + strlen(ch1));
// vector<char> veccol2(ch2, ch2 + strlen(ch2));
// vector<char> veccol3(ch3, ch3 + strlen(ch3));
// vector<char> veccol4(ch4, ch4 + strlen(ch4));
//
// vector<vector<char> > vecrow;
// vecrow.push_back(veccol1);
// vecrow.push_back(veccol2);
// vecrow.push_back(veccol3);
// vecrow.push_back(veccol4);
//
// vector<vector<char> > vec;
// cout << maximalSquare(vec);
// return 0;
// }
LeetCode: 221_Maximal Square | 二维0-1矩阵中计算包含1的最大正方形的面积 | Medium的更多相关文章
- LeetCode:搜索二维矩阵【74】
LeetCode:搜索二维矩阵[74] 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的 ...
- 01二维矩阵中最大全为1的正方形maxSquare——经典DP问题(二维)
在一个二维01矩阵中找到全为1的最大正方形 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 以矩阵中每一个点作为正方形右下角点来处理,而以该点为右下角点的最大边长最多比 ...
- LeetCode 74. 搜索二维矩阵(Search a 2D Matrix)
74. 搜索二维矩阵 74. Search a 2D Matrix 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. ...
- LeetCode 240. 搜索二维矩阵 II(Search a 2D Matrix II) 37
240. 搜索二维矩阵 II 240. Search a 2D Matrix II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性 ...
- Leetcode 240.搜索二维矩阵II
搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...
- Java实现 LeetCode 240 搜索二维矩阵 II(二)
240. 搜索二维矩阵 II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. ...
- Java实现 LeetCode 74 搜索二维矩阵
74. 搜索二维矩阵 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: ...
- leetcode 240搜索二维矩阵
/** 正常的二维搜索估计要超时,本题沿着对角线搜索,然后找到第一个大于目标数字的坐标(x,y)然后搜索(>x,<y)(<x,>y)子区域: 矩阵size() 为m,n:当i& ...
- leetcode 74 搜索二维矩阵 java
题目: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: mat ...
随机推荐
- Gradle命令行黑魔法
毫无疑问,现在Gradle已经成为java世界最火的构建工具,风头已经盖过了冗余的ant,落后的maven.Gradle是以Groovy语言编写的一套构建脚本的DSL,由于Groovy语法的优雅,所以 ...
- paip.快捷方式分组管理最佳实践ObjectDock
paip.快捷方式分组管理最佳实践ObjectDock /////挑选:除了od,还有个Berokyo ,但是bk无crash..只能使用1月.. Jumplist_Launcher_v7.2_rep ...
- paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService]
paip . 解决spring No unique bean of type [com.mijie.homi.search.service.index.MoodUserIndexService] ...
- Oracle数据库建表+添加数据练习
SQL脚本: --建表 --student表+注释 create table student( sno ) not null, sname ) not null, ssex ) not null, s ...
- jQuery实现左移右移
<html> <head> <meta charset="utf-8"> <title>完成左移右移</title> & ...
- 使用gulp进行React任务的构建
如果你不熟悉gulp的操作,可以看下下面的教程: gulp学习笔记1 gulp学习笔记2 gulp学习笔记3 gulp学习笔记4 对于gulp在react中的构建,找了很多资料,看了很多文章,也根据文 ...
- linux之cp/scp命令+scp命令详解(转)
名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... directory 说明:将一个档案拷贝至另一档案,或将数 ...
- windows server 2008 R2 FTP登陆错误。
建立了一个域用户ftp. 始终登陆不上winserver 2008 R2上的FTP. 错误如下: 530-User cannot log in. Win32 error: Logon failur ...
- 简单好用的sshfs -- 通过ssh映射远程路径(转)
最近习惯性访问N个Linux机器,在不同机器间跳来跳去,很是麻烦,最终,找到了sshfs,可以把远程目录直接映射到本地,无需修改远程机器的设置,仅要求有ssh连接的权限(ssh都没有的话,还能干啥?! ...
- 使用commons-beanutils迭代获取javabean的属性
NoteEntity entity = new NoteEntity(); entity.setNote001("a1"); entity.setNote002("a2& ...