动态规划例子: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)为右下角的面积最大的正方形的边长. 初始 ...
随机推荐
- mysql 问题排查语句
1.查询不是sleep或者有状态的sql select * from `information_schema`.processlist where command !='Sleep' or state ...
- 解决树莓派新版系统 ssh连接不了问题
一.解决办法 由于有连接了显示器,所以可以直接输入命令行开启树莓派的SSH,并且使用putty连接就可以. sudo service ssh start sudo service ssh sttus ...
- 转:mac环境下使用svn
在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...
- php中array_merge函数
php中array_merge函数 一.array_merge简介 (PHP 4, PHP 5, PHP 7) array_merge — 合并一个或多个数组 说明¶ array array_merg ...
- SAR(遥感、卫星) 图像常用数据集
Brazilian Coffee Scenes数据集较小,5MB左右: UC Merced Land Use Dataset(数据集规模较大,300MB+) MSTAR public targets ...
- 利用日志文件恢复MYSQL数据库
利用日志文件恢复MYSQL数据库 650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic ...
- C#开发 —— 泛型,文件
泛型的目标是采用广泛适用和可交互性的形式来表示算法和数据结构 —— 参数化 泛型能子啊编译时提供强大的类型检查,减少数据类型之间的显式转换,装箱操作和运行时的类型检查 泛型的类型参数T可以被看作是一个 ...
- 体验域名注册解析与SSL证书
- C语言-常量指针与指针常量
最近倪健问我一个问题,他说:什么是常指针?什么是指向常变量的指针?请举例说明 我查阅资料后这么回答他了, 指针常量(常指针):int * const p : 指针是一个常量,也就是说它始终指向那个地址 ...
- offSet和client和scroll
这三个是是js盒模型属性 client clientWidth 内容宽度加上左右padding clientHeight 内容高度加上上下padding clientTop 上边框的宽度 client ...