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 ...
随机推荐
- tomcat进程意外退出的问题分析(转)
原文链接:http://hongjiang.info/why-kill-2-cannot-stop-tomcat/ 节前某个部门的测试环境反馈tomcat会意外退出,我们到实际环境排查后发现不是jvm ...
- fetch body里数据为ReadableStream 解决办法
前端工程中发送 HTTP 请求从来都不是一件容易的事,前有骇人的 ActiveXObject ,后有 API 设计十分别扭的 XMLHttpRequest ,甚至这些原生 API 的用法至今仍是很多大 ...
- kdump+crash
参考:http://www.360doc.com/content/19/0205/08/36367108_813163495.shtml https://blog.csdn.net/u01436103 ...
- 转载:vsftp中的local_umask和anon_umask
转载出处:http://blog.sina.com.cn/s/blog_67c5699001010e3e.html umask是unix操作系统的概念,umask决定目录和文件被创建时得到的初始权限u ...
- 20144303 《Java程序设计》第一次实验实验报告
20144303 <Java程序设计>第一次实验实验报告 北京电子科技学院(besti)实验报告 课程:java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.04.08 ...
- SVN添加忽略目录
项目:Thinkphp 目录结构: Thinkphp |-- Common |-- Runtime |-- Home 忽略目标: Runtime 文件夹及下面所有文件 首先,需要忽略的目录必须没有加入 ...
- SaltStack日常维护-第七篇
练习内容 远程执行其他模块 官方模块有很多超过300+ 1.cmd.run 2.network 3.service 4.state 5.其它日常维护 演示 cmd.run模块 可以执行系统命令,超级模 ...
- XML CDATA是什么?
XML CDATA是什么? 投稿:mdxy-dxy 字体:[增加 减小] 类型:转载 这篇文章主要为大家介绍下XML CDATA是什么,学习xml的朋友可以参考下 All text in ...
- C语言一些总结
预处理#include<stdio.h>:头文件. stdio.h 标准输入输出. string.h 字符串预处理,. int main()主函数,返回的是int整型, return 0; ...
- Dokcer ELK
使用 docker 搭建 ELK 非常简单 docker run --name myes -d -p 9200:9200 -p 9300:9300 elasticsearch:2.4.4 运行 ...