动态规划例子: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)为右下角的面积最大的正方形的边长. 初始 ...
随机推荐
- .net 项目分层及规范
1.解决方案命名:公司简称+产品名称.如ABCSOft.BBS 2.解决方案文件夹:以数字排序例如:01.Web表示页面层:02.IBusinessLogic表示业务逻辑接口:03.Bussin ...
- rowcount和@@rowcount的区别
1 rowcount rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, set rowcount 10select * from 表A 这样的查询只会返回 ...
- PHP获取上周五时间简单写法
1.echo date('Y-m-d',strtotime('last friday')); 2.echo date("Y-m-d H:i:s",mktime(0, 0 , 0,d ...
- IDEA 官方教程
https://www.jetbrains.com/help/idea/discover-intellij-idea.html#UserInterface
- solr 亿万级数据查询性能測试
废话不多说,我电脑配置 i7四核cpu 8G内存 插入数据文档中有5个字段,当中有两个分词.一个int,一个date 批量插入測试一次10万循环10次总共100万用时85秒 批量插入測试一次10万循环 ...
- JAVA基础针对自己薄弱环节总结02(循环)
循环 A:水仙花. classShuiXianHua { public static void main(String[] args) { for(int i=101;i<1000;i++) { ...
- C#变量引用与全局变量
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 关于ajax访问express服务器的跨域问题
在学习es6的时候用promise封装了一个ajax <script type="text/javascript"> function getNews(URL) { l ...
- linux命令行打开图片
1.用预装的eog eog a.jpg 2.安装 imagemagick display a.jpg
- 从 QSplitter 中移除 QWidget(使用隐藏与显示,切换十分方便,不要真正销毁)
Splitter 的函数中有addWidget,但是却没有removeWidget, 或者delete之类的功能,所以如果想删去或者暂时不显示其中的某些widget就要自己手动完成这个效果.方法一:取 ...