动态规划例子: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)为右下角的面积最大的正方形的边长. 初始 ...
随机推荐
- 《一》安装 TP5
tp5 官方参考手册:http://www.kancloud.cn/manual/thinkphp5/118008 我这里采用的是 composer 安装,如果您没有安装 composer 的话 tp ...
- IDEA下使用maven的mybatis错误—XXXDao is not known to the MapperRegistry
# IDEA下使用maven的mybatis常见错误(二) 错误类型二:mybatis.xml注册映射文件错误 错误提示:Type interface com.aynu.dao.CountryDao ...
- Python——Pygame实现生命游戏(game of life)
模块:pygame import pygame,sys,time,random from pygame.locals import * """Color"&qu ...
- EditPlus 使用技巧以及快捷键
一边阅读,一边动手吧! 为了达到更好的效果,请你先下载我打包的这个 EditPlus压缩包文件(压缩包文件为绿色的EditPlus2.31英文版,含自动完成文件,高亮语法文件和剪切板代码片断文件,这些 ...
- 洛谷 P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银)
P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银) 题目描述 Farmer John owns NN cows with spots and NN cows wi ...
- javaWeb自己定义可排序过滤器注解,解决Servlet3.0下@WebFilter注解无法排序问题
com.lwl.anno 凝视类型 @WebFilterSort 须要用的jar包 http://download.csdn.net/detail/u013202238/9431110 用该注解注冊的 ...
- TaoCode-淘宝的SVN开源托管平台
无意中发现的..试用了一下,感觉还不错, 简单说一下怎样使用: 进入 http://code.taobao.org/project/explore/ 注冊完后依据提示新建项目,然后在本地随便新建一个文 ...
- jQuery UI炫酷雨滴落在水面上的波纹涟漪特效
raindrops是一款效果很炫酷的jQuery UI雨滴落在水面上的特效. 该特效模拟水滴从空中落入平静的水面上的效果. 通过參数能够设置水面波纹的大小,强度,波纹扩散的速度等等属性. 效果演示:h ...
- Oracle-02-数据库概述
一.数据库用途 用于存放数据的软件 当中Application server重要,将数据存在表中每一个表关系就能够反映不同表之间数据的关系,比方淘宝用户注冊.商品买卖等数据存在操作系统的目录中,不便于 ...
- Jmeter作为工具的性能测
[原创]相对完整的一套以Jmeter作为工具的性能测试教程(接口性能测试,数据库性能测试以及服务器端性能监测) 准备工作 jmeter3.1,为什么是3.1,因为它是要配合使用的serveragent ...