LeetCode Maximal Square
原题链接在这里:https://leetcode.com/problems/maximal-square/
题目:
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 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.
题解:
DP, 状态: 以当前点为右下角的最大都包含1的square边长.
转移方程 如果当前点为1, dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])+1.
初始化dp[m+1][n+1], 多一行一列方便处理边界.
res 一直取maintain dp的最大值.
优化dp成一行, 因为只需要左, 上, 和左斜上三个值. prev 来保存左斜上.
Time Complexity: O(m*n). m = matrix.length, n = matrix[0].length.
Space: O(n).
AC Java:
public class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return 0;
}
int res = 0;
int m = matrix.length;
int n = matrix[0].length;
int [] dp = new int[n+1];
int prev = 0;
for(int i = 1; i<=m; i++){
for(int j = 1; j<=n; j++){
int temp = dp[j];
if(matrix[i-1][j-1] == '1'){
dp[j] = Math.min(Math.min(dp[j], dp[j-1]), prev) + 1;
}else{
dp[j] = 0;
}
prev = temp;
res = Math.max(res, dp[j]);
}
}
return res*res;
}
}
LeetCode Maximal Square的更多相关文章
- 【动态规划】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 最大正方形
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
本来也想像园友一样,写一篇总结告别 2015,或者说告别即将过去的羊年,但是过去一年发生的事情,实在是出乎平常人的想象,也不具有代表性,于是计划在今年 6 月份写一篇 "半年总结" ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 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 Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [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 ...
随机推荐
- Python小例子
import urllib.request as request import urllib.parse as parse import string print(""" ...
- JavaScript验证函数大全
1. 长度限制 <script> function test() { if(document.a.b.value.length>50) { alert("不能超过50个字符 ...
- php函数描述及例子
/** * xml2array() will convert the given XML text to an array in the XML structure. * Link: http://w ...
- java抛出异常是怎么回事?
一.Java异常的基础知识 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的.比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如 ...
- Eclipse问题提示
记录下来以下两种方法: 一.只弹出简单的单词提示(如输入system.的时候自动弹出out.in等字段的那种): 点击eclipse上面的windows --> preferences --&g ...
- 彻底搞清楚字符编码: ASCII, ISO_8859, GB2312,UCS, Unicode, Utf-8
彻底搞清楚字符编码: ASCII, ISO_8859, GB2312,UCS, Unicode, U 1.ASCII: 0-127(128-255未使用),美国标准 2.IS0-8859-1(lati ...
- FZU 2124 bfs+vis记录
第一次团队训练赛的题 自己看完题没看到不能用舌头吃道具..以为是什么贪心混合bfs..果断放弃..悄悄的背锅了 然后其实比较简单 只是利用vis记录的时候要分两种状态记录 有没有道具 每到一个地方 就 ...
- mac下限速
sudo ipfw pipe 1 config bw 15KByte/s #设置带宽 sudo ipfw add 1 pipe 1 src-port 80 sudo ipfw add 1 pipe 1 ...
- 【FFXV】中物理模拟的结构以及游戏业界的乐趣
11月2日是在日本兵库县神户会议中心召开的[SIGGRAPH ASIA 2015]的第一天,在游戏开发专门的研究会[R&D in the Video Game Industry]上,展开了[F ...
- [分享]运维分享一一阿里云linux系统mysql密码修改脚本
[分享]运维分享一一阿里云linux系统mysql密码修改脚本 大象吃豆子 级别: 小白 发帖 12 云币 27 加关注 写私信 只看楼主 更多操作楼主 发表于: 2014-09-3 ...