LeetCode OJ: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
在给定的二维字符数组,找出最大全为1的矩形包含的1的个数:
注意这里相当于求面积而已,只要求出边长就很好办了。边长用dp很好求,这题其实跟前面有道dp问题很像,以后有时间找出来,先贴下代码:
class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(!matrix.size()||!matrix[].size()) return ;
vector<vector<int>>dp(matrix.size(), vector<int>(matrix[].size(), ));
int maxVal = ;
for(int i = ; i < matrix.size(); ++i){
if(matrix[i][] == ''){
dp[i][] = ;
maxVal = ;
}else dp[i][] = ;
}
for(int j = ; j < matrix[].size(); ++j){
if(matrix[][j] == ''){
dp[][j] = ;
maxVal = ;
}else dp[][j] = ;
}
for(int i = ; i < matrix.size(); ++i){
for(int j = ; j < matrix[].size(); ++j){
if(matrix[i][j] == ''){
dp[i][j] = min(dp[i-][j], min(dp[i][j-], dp[i-][j-])) + ;
maxVal = max(maxVal, dp[i][j]);
}else
dp[i][j] = ;
}
}
return maxVal*maxVal;
}
};
LeetCode OJ:Maximal Square(最大矩形)的更多相关文章
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [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 ...
- 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 ...
- (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 _ 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之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 OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
随机推荐
- spark[源码]-sparkContext概述
SparkContext概述 sparkContext是所有的spark应用程序的发动机引擎,就是说你想要运行spark程序就必须创建一个,不然就没的玩了.sparkContext负责初始化很多东西, ...
- Linux进程管理 lsof命令:列出进程调用或打开的文件信息
lsof命令 通过 ps 命令查询到系统中所有的进程, 通过lsof 命令可以知道这个进程到底在调用哪些文件.lsof 命令格式如下: [root@localhost ~]# lsof [选项] 选项 ...
- ABP官方文档翻译 1.4 启动配置
启动配置 配置ABP 替换内置服务 配置模块 创建模块配置 ABP提供了基础设施和模型在启动的时候对它及模块进行配置. 配置ABP 在模块的PreInitialize事件中配置ABP.示例配置如下: ...
- chrome 获得点击按钮时的事件
初次了解浏览器高级点的功能,原来这么强 想了解点击一个网页的按钮时触发了什么事件,firefox chrome确实很强大,基本可以监控所有内容 以chrome为例: 在按钮上 右键检查 或者 F12 ...
- 20145313Java第一次实验
实验内容 1.JVM.JRE.JDK的安装位置与区别: 2.命令行运行javac:java:javac -cp; java -cp: 3.PATH,CLASSPATH,SOURCEPATH的设定方法与 ...
- 33c3-pwn500-recurse
Recurse 好记性不如烂笔头.当时没有记录,现在趁着有时间简单写一写,为以后留备份. 这个题目当时并没有队伍做出来,赛后作者发布了题目的源码和解答.看了之后发现是一个UAF漏洞,不过漏洞很不好找. ...
- JavaScript中字符操作之大小写转换
1.toUpperCase() 方法用于把字符串转换为大写 var str = prompt("请输入需转换大写的字符串:"); str = str.toUpperCase() ...
- 彻底的卸载干净oracle 11g
1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit> H ...
- cogs 1487. 麻球繁衍
★ 输入文件:tribbles.in 输出文件:tribbles.out 评测插件 时间限制:3 s 内存限制:256 MB [题目描述] 万有引力定律: “使物体相互靠近的力的大小与 ...
- mysql对数据库的备份和还原
在对mysql数据库的某个数据库进行备份时,使用 mysqldump命令来进行操作 mysqldump -u root -p db_database_name > /[your_path.mys ...