动态规划例子: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
Return 4.
public class Solution {
public int maximalSquare(char[][] matrix) {
int rows = matrix.length;
if(rows == 0) return 0;
int cols = matrix[0].length;
if(cols == 0) return 0;
Node[][] sta = new Node[rows][cols];
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
sta[i][j] = new Node();
}
}
int res = 0;
if(matrix[0][0] == '1'){
sta[0][0].left = 1;
sta[0][0].up = 1;
sta[0][0].maxSize = 1;
res = 1;
}
//求第一行
for(int i=1; i<cols; i++){
if(matrix[0][i] == '1'){
sta[0][i].left = sta[0][i-1].left+1;
sta[0][i].maxSize = 1;
res = 1;
}
}
//求第一列
for(int j=1;j<rows;j++){
if(matrix[j][0] == '1'){
sta[j][0].up = sta[j-1][0].up + 1;
sta[j][0].maxSize = 1;
res = 1;
}
}
//动态求其他
for(int i=1; i<rows; i++){
for(int j=1; j<cols; j++){
if(matrix[i][j] == '1'){
sta[i][j].left = sta[i-1][j].left + 1;
sta[i][j].up = sta[i][j-1].up + 1;
sta[i][j].maxSize = 1;
if(matrix[i-1][j-1] == '1'){
sta[i][j].maxSize = Math.min(sta[i][j].left, sta[i][j].up);
sta[i][j].maxSize = Math.min(sta[i][j].maxSize, sta[i-1][j-1].maxSize+1);
}
}
res = Math.max(sta[i][j].maxSize, res);
}
}
return res*res;
}
class Node{
int left;
int up;
int maxSize;
}
}
动态规划例子:Maximal Square的更多相关文章
- LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle
1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...
- 【动态规划】leetcode - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [LintCode] 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
问题描述: 题目链接: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 ...
- 221. Maximal Square(动态规划)
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- 动态规划-最大的正方形面积 Maximal Square
2018-09-13 19:19:44 问题描述: 问题求解: 方法一: 使用动态规划来求解,算法时间复杂度O(n^2). dp[i][j] : 以(i, j)为右下角的面积最大的正方形的边长. 初始 ...
随机推荐
- ireport 追加新报表
ireport 追加新报表 /* To change this template, choose Tools | Templates * and open the template in the e ...
- Sub Thread to update main Thread (UI)
Sub Thread to update main Thread (UI) main Thread : A has Hander.HandleMessage() to process the & ...
- 分享一个tom大叔的js 深入理解系列 (有助于提升)
http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html#3620172
- Network Stack : HTTP Cache
HTTP Cache 目录 1 Operation 2 Sparse Entries 3 Truncated Entries 4 Byte-Range Requests 5 HttpCache::Tr ...
- 【Django】ORM操作#2
目录 必知必会的13条查询方法 单表查询之神奇的双下划线 一对多 ForeignKey 多对多 ManyToManyField 在Python脚本中调用Django环境 Django终端打印SQL语句 ...
- 【iOS开发-27】解决方式:An error was encountered while running(Domain=FBSOpenApplicationErrorDomain, Code=4)
iOS simulator出现故障,提示: An error was encountered while running (Domain = FBSOpenApplicationErrorDomain ...
- Linux监控(OS,MySQL,Tomcat,Apache)
关于逐步脱离开发岗位的意见,老大已经批准了,接下来我的主要工作就是"运维+数据库管理".感谢杰民兄和小马哥能接受我的骚扰.接下来还会去骚扰他们,同一时候也会去骚扰董大爷,小刚总,心 ...
- 最简单的基于FFmpeg的移动端样例:Android 视频转码器
===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...
- RTSP传输协议之Methods总结
RTSP/1.0 200 OK Server: DSS/5.5.5 (Build/489.16; Platform/Linux; Release/Darwin; state/beta; ) Cseq: ...
- idea git ignore 插件
https://blog.csdn.net/qq_34590097/article/details/56284935