【刷题-LeetCode】221. Maximal Square
- Maximal Square
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
解 动态规划,设\(dp[i][j]\)表示下标为(i, j)的左上角区域的最大正方形面积,当\(\mathrm{matrix}[i][j]\neq '0'\)时:
\]

class Solution {
public:
int maximalSquare(vector<vector<char>>& matrix) {
if(matrix.size() == 0)return 0;
int m = matrix.size(), n = matrix[0].size();
int *dp = new int[n+1];
memset(dp, 0, (n+1)*sizeof(int));
int ans = 0, prev = 0;
for(int i = 1; i <= m; ++i){
prev = dp[0];
for(int j = 1; j <= n; ++j){
int tmp = dp[j];
if(matrix[i-1][j-1] == '1'){
dp[j]=min(dp[j], min(prev, dp[j-1]))+1;
ans = max(ans, dp[j]);
}else{
dp[j] = 0;
}
prev = tmp;
}
}
return ans*ans;
}
};
【刷题-LeetCode】221. Maximal Square的更多相关文章
- 求解最大正方形面积 — 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 221. Maximal Square
本题用brute force超时.可以用DP,也可以不用. dp[i][j] 代表 以(i,j)为右下角正方形的边长. class Solution(object): def maximalSquar ...
- 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刷题------------------------------LeetCode使用介绍
临近毕业了,对技术有种热爱的我也快步入码农行业了,以前虽然在学校的ACM学习过一些算法,什么大数的阶乘,dp,背包等,但是现在早就忘在脑袋后了,哈哈,原谅我是一枚菜鸡,为了锻炼编程能力还是去刷刷Lee ...
随机推荐
- java 编程基础:注解(Annotation Processing Tool)注解处理器 利用注解解读类属性生成XML文件
APT的介绍: APT(Annotation Processing Tool)是一种注解处理工具,它对源代码文件进行检测,并找出源文件所包含的注解信息,然后针对注解信息进行额外的处理. 使用APT工具 ...
- 1632 B君的连通
B国拥有n个城市,其交通系统呈树状结构,即任意两个城市存在且仅存在一条交通线将其连接.A国是B国的敌国企图秘密发射导弹打击B国的交通线,现假设每条交通线都有50%的概率被炸毁,B国希望知道在被炸毁之后 ...
- 应用程序开发 WebApp NativeApp 微信小程序
Web Native App 微信小程序 WebApp是指基于Web的系统和应用,其作用是向广大的最终用户发布一组复杂的内容和功能.webapp 框架是一种简单的与WSGI兼容的网络应用程序框 ...
- spring练习,使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出。
相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出.要求如下: ...
- Java高级程序设计作业目录(作业笔记)
持续更新中............. Java高级程序设计笔记 • [目录] 我的大学笔记>>> 第1章 IO流>>> 1.1.3 编写Java程序,在电脑硬盘里, ...
- Linux操作系统RedHat6.5安装
1.说明 安装Linux操作系统Red Hat 6.5, 安装镜像为rhel-server-6.5-x86_64-dvd.iso. 2.开始安装 在BIOS里设置成从光驱启动, 服务器上电后会加载光驱 ...
- 很漂亮的一个背景控件——ribbons.js
写博客的人都喜欢优化自己的博客主页,博主也一样,找了一些背景控件,像canvas-nest.js等等,最终选择了ribbons.js,并基于源码,稍作了一丁点的修改,这里分享出来 (function ...
- js 盒子逐渐缓慢移动效果
注释:可以用于盒子弹出,收回效果,比如:某东的产品详情页,侧边有购物车.优惠卷等,鼠标经过弹出效果 可以看这个网址使用案例:https://www.cnblogs.com/jq-growup/p/15 ...
- Dubbo服务 上传文件解决方案以及Hessian协议
协议支持 Dubbo支持多种协议,如下所示: Dubbo协议 Hessian协议 HTTP协议 RMI协议 WebService协议 Thrift协议 Memcached协议 Redis协议 在通 ...
- git clone 失败 ,提示 fatal: unable to access 'https://github.com/xxx.git/': OpenSSL SSL_read: Connection was reset, errno 10054
怎么解决? 把原来的指令 $ git clone https://github.com/cen-xi/express.git 改成 $ git clone git://github.com/cen-x ...