Maximal Square 解答
Question
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.
Solution
2-D array. The changing condition is:
t[i][j] = min(t[i][j-1], t[i-1][j], t[i-1][j-1]) + 1.
public class Solution {
public int maximalSquare(char[][] matrix) {
if (matrix == null || matrix.length < 1)
return 0;
int row = matrix.length, column = matrix[0].length, max = 0;
int[][] dp = new int[row][column];
// Top Row
for (int i = 0; i < column; i++) {
if (matrix[0][i] == '0')
dp[0][i] = 0;
else
dp[0][i] = 1;
}
// Left Column
for (int i = 0; i < row; i++) {
if (matrix[i][0] == '0')
dp[i][0] = 0;
else
dp[i][0] = 1;
}
// Inside Filling
for (int i = 1; i < row; i++) {
for (int j = 1; j < column; j++) {
if (matrix[i][j] == '0') {
dp[i][j] = 0;
} else {
int tmp = Math.min(dp[i][j - 1], dp[i - 1][j]);
dp[i][j] = Math.min(tmp, dp[i - 1][j - 1]) + 1;
}
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++)
max = Math.max(max, dp[i][j]);
}
return max * max;
}
}
Maximal Square 解答的更多相关文章
- 求解最大正方形面积 — leetcode 221. Maximal Square
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- [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 - Maximal Square
称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...
- 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】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- 【刷题-LeetCode】221. Maximal Square
Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing ...
- [LeetCode] 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 Maximal Square
原题链接在这里:https://leetcode.com/problems/maximal-square/ 这是一道DP题,存储历史信息是到当前点能有的最大square, 用二维数组dp存储. 更新方 ...
随机推荐
- bzoj1060 [ZJOI2007]时态同步
Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板 ...
- 【HDU1102】Constructing Roads(MST基础题)
最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...
- 04747_Java语言程序设计(一)_第8章_多线程
例8.1应用程序用Thread子类实现多线程. import java.util.Date; public class Example8_1 { static Athread threadA; sta ...
- 【转】Linux系统调用列表
一.进程控制: fork 创建一个新进程 clone 按指定条件创建子进程 execve 运行可执行文件 exit 中止进程 _exit 立即中止当前进程 getdtablesize 进程所能打开的最 ...
- 学习php常用算法
<?php /*学用php算法*/ /*1.冒泡法 *思路分析:在要排序的一组数中,对当前还未排好的序列, *从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒. *即,每 ...
- CharacterController 角色控制器实现移动和跳跃
之前我使用SimpleMove来控制角色的移动, 后来又想实现人物的跳跃, 看见圣典里面是使用Move来实现的. =.= 然后我都把他们改成move来实现了 代码实现: using UnityEngi ...
- Unity 弹道轨迹
using UnityEngine; using System.Collections; public class ProjectileTest : MonoBehaviour { public Ga ...
- 用CSS3实现对图片的放大效果
用CSS3对图片放大效果 .right_div .topicons li a:hover img{ -webkit-transform:scale(1.5,1.5); -moz-tra ...
- Vlc for Android 全面阐述
简单介绍 Vlc for android是一款开源安卓播放器.具备播放多媒体文件.光盘.设备以及网络流媒体协议等功能,支持ARMv7 CPU或一个x86 CPU的设备,全部播放控制特性都已经开发完整. ...
- Impala与Hive的比較
1. Impala架构 Impala是Cloudera在受到Google的Dremel启示下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的Hive+MapReduce批 ...